Implementation of the interface

Source: Internet
Author: User
Tags bind

The implementation of 15.4.1 class to interface

As we have said earlier, the interface definition does not include the implementation part of the method. An interface can be implemented by a class or struct. We mainly describe the implementation of interfaces through classes. When you implement an interface with a class, the name of the interface must be included in the list of base classes in the class declaration.

The following example shows an example of an interface implemented by a class. Where isequence is a queue interface that provides a member method add () that adds an object to the end of the queue, iring is a circular table interface that provides a method of inserting an object into the loop (object obj), and the method returns the insertion position. The class Ringsquence implements interface Isequence and interface iring.

Program Listing 15-4:

Using System;
Interface Isequence
{
 object Add ();
}
Interface iring
{
 int Insert (object obj);
}
Class ringsequence:isequence,iring
{Public
 Object Add () {...}
      public int Insert (object obj) {...}
}

If a class implements an interface, the class implicitly inherits all the parent interfaces of the interface, regardless of whether the parent interfaces are listed in the base class list of the class declaration.

Using System;
Interface icontrol
{
 void Paint ();
}
Interface Itextbox:icontrol
{
 void SetText (string text);
}
Class Textbox:itextbox
{public
 void Paint () {...}
 public void SetText (string text) {...}}

In this case, the class textbox not only implements the interface ITextBox, but also implements the parent interface icontrol of the interface ITextBox.

As we've seen earlier, a class can implement multiple interfaces. Look at the following example:

Using System;
Interface icontrol
{
 void Paint ();
}
Interface IDataBound
{
 void Bind (Binder b);
}
public class Control:icontrol
{public
 void Paint () {...}}
}
public class Editbox:control,icontrol,idatabound
{public
 void Paint () {...}
 public void Bind (Binter b) {...}
}

In the example above, class EditBox inherits from the control class and implements both the IControl and IDataBound interfaces. The paint method in EditBox comes from the IControl interface, and the Bind method comes from the IDataBound interface, both of which are implemented as public members in the EditBox class. Of course, in C # we can also choose not to implement interfaces as public members.

If each member clearly points to the implemented interface, the interface implemented through this approach is called an explicit interface member (explicit interface). In this way we rewrite the above example:

public class Editbox:icontrol,idatabound
{
 void IControl.Paint () {...}
 void IDataBound.Bind (Binder b) {...}}
Explicit interface members can only be invoked through an interface. For example:

class Test
{
 static void Main () {
    editbox editbox=new editbox ();
    EditBox. Paint (); Error:no Such method
    icontrol Control=editbox;
    Control. Paint (); Calls EditBox ' s Paint implementation
 }
}

The above code is for EditBox. The call to Paint () is incorrect because the editbox itself does not provide this method. Control. Paint () is the correct way to invoke.

Note: The interface itself does not provide the implementation of the defined member, it simply describes those members who must rely on the support of the class or other interface that implements the interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.