Class implementation of the interface
As we have said, the interface definition does not include the implementation of the method. The interface can be implemented by a class or structure. We mainly tell the interface to implement the interface. When using a class to implement an interface, the name of the interface must be included in the list of base class in class definition.
The following example gives an example of an interface by a class. Where ISEQUENCE is a queue interface, it provides member methods add () add (), iRing to a recurring table interface to the queue, providing the method INSERT (Object Obj) inserted into the ring, and the method returns to the inserted position. Class RingsQuence implements interface ISEQUENCE and interface Iring.
using System; interface ISequence {object Add ();} interface ISequence {object Add ();} interface IRing {int Insert (object obj);} class RingSequence: ISequence, IRing {public object Add () {...} public int Insert (Object obj) {...}}
If the class implements an interface, the class also implicit all parent interfaces of the interface, regardless of whether these parent interfaces are listed in the base class table defined by the class definition. Look at the example below:
using System; interface IControl {void Paint ();} interface ITextBox: IControl {void SetText (string text);} interface IListBox: IControl {void SetItems (string [] items);} interface IComboBox: ITextBox, IListBox {}
Here, interface ICOMBOBOX inherits ITextBox and IListbox. Class TextBox not only implements interface ITextbox, but also implements the parent interface Icontrol of interface ITextBox.
We have seen before, a class can implement multiple interfaces. Look at the example below:
Interface Idatabase (Binder B); PUBLIC CLASS Editbox: Control, Icontrol, Idatabase; PUBLIC VOID (Binder B) {...}}
Class Editbox derived from class Control and implemented Icontrol and Idatabase. In the previous example, the PAINT method in the interface iControl and the BIND method in the iDatabase interface are implemented in public members in class Editbox. C # provides an alternative way to implement these methods, which can be made to the act of performing these members into public. Interface members can be implemented with a valid name. For example, class editbox can be implemented by the method Icontrol.Paint and iDatabase. Bind.
Public class editbox: icontrol, iDatabase {void icontrol.paint () {...} void iDatabasend.bind (binder b) {...}}
Since each member is implemented by an external assignment interface member, the member implemented by this method is referred to as an external interface member. External interface members can only be called by an interface. For example, the implementation of Editbox in the PAINT method can be called only by creating an IControl interface.
Class test {static void main () {editbox editbox = new editbox (); editbox.paint (); // error: editbox does not have a Paint event icontrol control = editbox; control.paint (); // Call EditBox Paint Event} } In the above example, class Editbox inherits from the Control class and implements the Icontrol and Idatabase interface. The PAINT method in Editbox comes from the icontrol interface, and the BIND method comes from the iDatabase interface, both of which are implemented as public members in the Editbox class. Of course, we can also choose to implement interfaces as public members in C #.
If each member clearly points out the implemented interface, the interface implemented by this way we call the explicit interface member (Explicit Interface Member). Use this way we rewrite the above example:
Public class editbox: icontrol, iDatabase {void icontrol.paint () {...} void iDatabasend.bind (binder b) {...}}
Explicit interface members can only call through interfaces. E.g:
Class ctest {static void main () {editbox editbox = new editbox (); editbox.paint (); // error: different ways icontrol control = editbox; control.paint (); // Call Editbox's Paint Method}}
The call to editbox.paint () in the above code is wrong because the EditBox itself does not provide this method. Control.paint () is the correct call mode.
Note: The interface itself does not provide the implementation of the defined member, which only illustrates these members, which must rely on the support of the interface or other interfaces that implement the interface.
Know how to access the interface, we also know how to implement the interface, to implement the C # interface, please see the next section - Implement the interface