Implement interface 1

xiaoxiao2021-03-06  56

Section 5, implement interface 1, explicitly implement interface members In order to implement interfaces, classes can define explicit interface member executives (Explicit Interface Member Implementations). Explicit Interface Members can be a method, an attribute, an event or a definition of an index indicator, defining the consistency of the full name corresponding to the member.

using System; interface ICloneable {object Clone ();} interface IComparable {int CompareTo (object other);} class ListEntry: ICloneable, IComparable {object ICloneable.Clone () {...} int IComparable.CompareTo (object other) {...} }

In the above code, iCloneable.clone and IComparable.Compareto are explicit interface member executives.

Description:

1. You cannot access the explicit interface member executter by the full name of the explicit interface in the method call, attribute access, and index indicator access. In fact, the explicit interface member executive can only be accessed by reference to an instance of the interface.

2. Explicit Interface Members cannot use any access restrictions, or add Abstract, Virtual, Override or Static modifiers.

3, explicit interface member executives and other members have different ways. The explicit interface member executor is private in a sense because it cannot be accessed through the full name of the name, and the index indicator access. But they can also be accessed by an instance of the interface, but also have a certain public property.

4. Only class is in defined, write the interface name in the base class list, and the full name, type, and return type defined in the class are exactly the same as the explicit interface member executive, the explicit interface member executive It is effective, for example:

Class show: iCloneable {Object iCloneable.clone () {...} int icompable.compareto (Object Other) {...}}

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

New Post(0)