Section fourth, Access interface
access to interface members
The same is true of the invocation of an interface method and the rules that take access to an index indicator as in a class. If the name of the underlying member is the same as the high-level member inherited, the underlying member overwrites the top-level member with the same name. However, because the interface supports multiple inheritance, in multiple inheritance, if two parent interfaces contain members of the same name, this produces two of semantics (which is one of the reasons why C # removes multiple inheritance mechanisms for classes), which requires explicit definition:
Using System;
Interface Isequence {
int Count {get; set;}
}
Interface Iring {
void Count (int i);
}
Interface Iringsequence:isequence, iring {}
Class CTest {
void Test (Iringsequence rs) {
Rs. Count (1); Error, Count has two meanings
Rs. Count = 1; Error, Count has two meanings
((isequence) RS). Count = 1; That's right
((iring) RS). Count (1); Call Iring.count correctly
}
}
in the example above, the first two statements are Rs. Count (1) and Rs. Count = 1 causes a compile-time error because of two semantics, so you must explicitly assign the parent interface type to RS, which does not incur additional overhead at run time.
look at the following example:
Using System;
Interface Iinteger {
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); That's right
((Iinteger) n). ADD (1); That's right
((idouble) n). ADD (1); That's right
}
}
calling Num.add (1) results in two semantics because the parameter types of the candidate overloaded methods apply. However, calling Num.add (1.0) is allowed because 1.0 is inconsistent with the parameter type of the floating-point number parameter type and method Iinteger.add (), at which point only idouble.add is applicable. However, as long as the explicit assignment is added, no two semantics will be generated.
problems with multiple inheritance of interfaces can also result in member access issues. For example:
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
}
}
Total 3 page: previous 1 [2] [3] Next page