Implement interface 6

xiaoxiao2021-03-06  58

Pay attention to the following two points when performing interface mapping:

1. When deciding which member of the class is implemented, the interface member in the class is prioritized than other members.

2. Members using private, protected, and static modifiers cannot participate in implementation interface mapping. E.g:

Interface iCloneable {Object Clone ();} class c: iCloneable {Object iCloneable.clone () {...} public object clone () {...}}

Examples of Members Icloneable.clone are implemented by members of Interface Icloneable, because it is an explicitly explained interface member, which has higher priority than other members.

If a class implements two or more interfaces, types, and parameter types, one member in the class may implement all of these interface members:

Interface iControl {void Paint (); interface means {void pressure ();} class page: icontrol, iForm {public void point () {...}}

Here, both interface Icontrol and IFORM methods Paint are mapped to the PAINT method in class Page. Of course, these two methods can be implemented separately by explicit interface members:

Interface icontrol {void Paint ();} interface iform {void pressure ();} class page: icontrol, iForm {public void icontrol.paint () {// specific interface implementation code} public void iform.paint () {/ / Concrete interface implementation code}}

The above two kinds of writing are correct. However, if the interface member covers the member of the parent interface in the inheritance, the implementation of the member may have to map to the explicit interface member executive. Look at the example below:

Interface ibase {Int p {get;}} interface id;} ipase {new int p ();

Interface IDerived Inherited from Interface IBase, when the member method of interface IDerived overwrites the membership method of the parent interface. Because there is a member of the same name, the implementation of the two interface members will not be able to distinguish the interface mapping. So, if a class is to implement interface IDerived, you must define at least one explicit interface member executive. It is reasonable to use these writing methods:

// 1: Both the two interface members use explicit interface members to implement the LASS C: IDerived {INT IBASE.P GET {// Specific Interface Implementation Code} int iderived.p () {// Specific interface Implementation code}} // 2: Interface members of IBASE use explicit interface members to implement Class C: iDerived {int ibase.p GET {// Specific interface implementation code} public int P () {// specific Interface Implementation Code}} // Three: Interface members of iDerived use explicit interface members to implement Class C: IDerived {public int P get {// specific interface implementation code} int idherd.p () {/ / Concrete interface implementation code}}

Another situation is that if a class implements a plurality of interfaces, these interfaces have the same parent interface, and this parent interface is allowed to be implemented once.

using System; interface IControl {void Paint (); interface ITextBox: IControl {void SetText (string text);} interface IListBox: IControl {void SetItems (string [] items);} class ComboBox: IControl, ITextBox, IListBox {void IControl .Paint () {...} void ITEXTBOX.SETTEXT (STRING TEX) {...} Void ilistbox.setitems (String [] items) {...}} The above example, class ComboBox implements three interfaces: Icontrol, ITextBox and IListbox . If ComboBOX not only implements the IconTrol interface, but also achieves their parent interface icontrol while implementing ITEXTBOX and IListbox. In fact, the implementation of interface ITEXTBOX and ILISTBOX, shared the implementation of the interface Icontrol.

We have a comprehensive understanding of the C # interface, basically how to apply C # interface programming, but in fact, C # is not only applied to the .NET platform, it also supports the previous COM, you can implement COM class to .NET Class conversion, such as C # calling the API. For knowledge, please see the next section - interface conversion.

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

New Post(0)