2, inheritance interface implementation
The interface has no variability, but this does not mean that the interface is no longer developed. Similar to the inheritance of the class, the interface can also inherit and develop.
Note: Interface inheritance and class inheritance, first, class inheritance is not only inheritance, but also to achieve inheritance; and interface inheritance is just a manifold. That is, the derived class can inherit the method of the base class, while the derived interface only inherits the member method of the parent interface, without the implementation of the parent interface, secondly, C # Class inheritance is only allowed to inherit, but the interface inheritance Allow multiple inheritance, one sub-interface can have multiple parent interfaces.
The interface can be inherited from zero or multiple interfaces. When inheriting from multiple interfaces, use ":" to follow the inherited interface name, multiple interface names, ",". The inherited interface should be accessible, such as inheriting from a Private type or an Internal type interface, is not allowed. The interface is not allowed to inherit itself directly or indirectly. Similar to the inheritance of the class, the inheritance of the interface also forms the hierarchy between the interfaces.
Please see the example below:
using System; interface IControl {void Paint ();} interface ITextBox: IControl {void SetText (string text);} interface IListBox: IControl {void SetItems (string [] items);} interface IComboBox: ITextBox, IListBox {}
The inheritance of an interface also inherits all members of the interface, and the interface ITEXTBOX and IListbox are inherited from the interface iControl in the example, which inherits the PAINT method of the interface Icontrol. Interface ICOMBOBOX is inherited from interface ITextBox and IListbox, so it should inherit the interface ITExtBox's setText method and IListbox's setItems method, as well as Icontrol's Paint method.
A class inherits all interfaces that are provided by its basic classes.
A derived class cannot be changed from the interface mapping of its basic class inheritance by explicitly implementing an interface. For example, in the declaration
Interface icontrol {void Paint ();} class control: icontrol {public void point () {...}} class textbox: control {new public void point () {...}}
The method in TextBox is hidden in Control Paint, but there is no change from the mapping from Control.Paint to Icontrol.Paint, and Paint will have the following effects through class instances and interface instances.
Control c = new control (); TextBox t = new textbox (); icontrol IC = C; icontrol it = t; c.paint (); // affects control.paint (); T.Paint (); // TextBox.paint (); IC.Paint (); // affects control.paint (); it.paint (); // affects control.paint ();
However, when an interface method is mapped to a virtual method in a class, derived classes cannot overwrite this virtual method and changing the implementation function of the interface. For example, rewrite the above statement as
Interface icontrol {void Paint ();} class control: icontrol {public virtual void point () {...}} class textbox: control {public override void Paint () {...}} will see the results below :
Control c = new control (); TextBox t = new textbox (); icontrol IC = C; icontrol it = t; c.paint (); // affects control.paint (); T.Paint (); // TextBox.paint (); ic.paint (); // affects control.paint (); it.paint (); // affects TextBox.paint ();
If the explicit interface member implementation procedure cannot be declared as virtual, it is impossible to overwrite an explicit interface member implementation program. An explicit interface member implementation program calls another method is effective, while the other method can be declared as virtual so that the derived class can overwrite it. E.g:
interface IControl {void Paint ();} class Control: IControl {void IControl.Paint () {PaintControl ();} protected virtual void PaintControl () {...}} class TextBox: Control {protected override void PaintControl () { ...}}
Here, the class inherited from Control can be specialized for Icontrol.Paint implementation programs by coverage method PaintControl.