Access interface 1

xiaoxiao2021-03-06  53

Section IV, access to the interface to interface members

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 IRing {void Count (int i);} interface IRingSequence: ISequence, IRing {} class CTest {void Test (IRingSequence rs) {//rs.Count ( 1); Error, count has an amphibian //rs.count = 1; error, count has an unisowed (ISEQUENCE) RS) .count = 1; // correct ((ign) rs) .count (1) ; / / Conduct IRing.count}} correctly

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 inumr: 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 ion ibase {new void fway (int i);} interface iright: ibase {void g ();} interface idherive: ingft, ratface {} 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}}

转载请注明原文地址:https://www.9cbs.com/read-83894.html

New Post(0)