Section Fifth, implementing interfaces
1. Explicitly implement interface member
in order to implement an interface, a class can define an explicit interface member executor (EXPLICIT Interface membership implementations). An explicit interface member Executor can be a method, a property, an event, or a definition of an index indicator, which should be consistent with the full name of 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) {...}
}
the ICloneable.Clone and IComparable.CompareTo in the code above are explicit interface member execution bodies.
Description:
1. An explicit interface member executor cannot be accessed through a full name in method invocation, property access, and index indicator access. In fact, an explicit interface member Executor can only be accessed through an instance of an interface, referencing only the member name of the interface.
2. An explicit interface member executor cannot use any access limiter or add abstract, virtual, override, or static modifiers.
3, the explicit interface member Executive Body and other members have different access methods. Explicit interface member execution is private in a sense because it cannot be accessed through a full name in method invocation, property access, and index indicator access. But they can also be accessed through an instance of the interface and have a certain public character.
4, when the class is defined, the interface name is written 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 executing body, and the explicit interface member executor is valid, for example:
Class Shape:icloneable {
Object ICloneable.Clone () {...}
int IComparable.CompareTo (object other) {...}
}
Using an explicit interface member executor typically has two purposes:
1, because the explicit interface member execution body cannot be accessed through the class instance, this can separate the implementation part of the interface from the public interface. If a class uses the interface only internally, and the user of the class does not use the interface directly, the explicit interface member Executor can play a role.
2. Explicit interface member execution avoids confusion between interface members because of the same name. If a class wants different implementations of interface members with the same name and return type, this must be done using the explicit interface member execution body. If there is no explicit interface member executor, the class cannot be implemented for interface members that are different from the name and return type.
The following definition is not valid because the interface IComparable is not present in the base class list when the shape is defined.
Class Shape:icloneable
{
Object ICloneable.Clone () {...}
}
Class Ellipse:shape
{
Object ICloneable.Clone () {...}
}
Total 7 Pages: prev 1 [2] [3] [4] [5] [6] [7] Next page