Section IV, access interface
Access to the interface member
The invocation of the interface method is also the same as the rules accessed by the index indicator. If the base member is named, the underlying member will cover the high-level members of the same name. However, because the interface supports more inheritance, in the multi-inheritance, if the two parent interfaces contain members of the same name, this produces an erriness (this is also one of the reasons for the multi-inheritance mechanism of classes in C #). Explicit definitions are required:
Using system;
Interface ISEQUENCE {
INT count {get; set;}
}
Interface {{
Void Count (INT I);
}
Interface IringSequence: ISEQUENCE, IRING {}
Class ctest {
Void test (iringsequence q) {
//rs.count (1); error, count is unsatisfactory
//rs.count = 1; error, count has an unity
(ISEQUENCE) RS) .count = 1; // correct
((Iling) rs) .count (1); // Conduct IRING.COUNT
}
}
In the above example, the first two statements rs .count (1) and rs .count = 1 generate erlies, resulting in compile time errors, so it must explicitly assign the parent interface type, this assignment is running Will not bring additional overhead.
Look at the example below:
Using system;
Interface integer {
Void Add (INT I);
}
Interface idouble {
Void Add (Double D);
}
Interface Inumber: Iinteger, iDouble {}
Class CMYTEST {
Void test (Inumber Num) {
// Num.Add (1); error
Num.add (1.0); // correct
(Integer) n) .add (1); / / correct
(IDouble) n) .add (1); // correct
}
}
Call Num.Add (1) will result in an amphony because the parameter type of the candidate is applicable. However, call Num.Add (1.0) is allowed because 1.0 is inconsistent with the parameter type of floating point number parameter type and method Iinteger.Add (), and only iDouble.Add is applicable. However, as long as the explicit assignment will never produce.
The problem of multiple inheritance of the interface also brings a problem with members visiting. E.g:
Interface ibase {
Void Fway (INT I);
}
Interface Ileft: ibase {
New Void Fway (INT I);
}
Interface Iright: IBASE
{void g ();
Interface IDerived: Ileft, Iright {}
Class ctest {
Void test (iderived d) {
d. fway (1); // Call Ileft. fway
((Ibase) d). Fway (1); // Call IBase. Fway
((Ileft) d). Fway (1); // Call Ileft. Fway
(IRIGHT) D). Fway (1); // Call IBase. Fway
}
}
In the above example, IBASE.FWAY covered with the member method of Ileft in the derived interface ILEFT. So the call to d. Fway (1) is actually called. Although from IBase-> IRIGHT-> iDerived inheritance path, the ileft.fway method is not covered. We only need to remember this: Once the member is overwritten, all the access to its access is overwritten "intercepted". 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 {{
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;
}
Interface ICOBOX: 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 iDatabaseD {
Void Bind (Binder B);
}
Public Class Editbox: Control, Icontrol, Idatabase {
Public void paint ();
Public void bind (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 (); // Calling Editbox's PAINT event
}
}
In the above, 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 methods
Icontrol control = editbox;
Control.paint (); // Calling EditBox 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