C # Interface Basics Tutorial Five implementation interface

Source: Internet
Author: User
Tags ibase modifiers

1. Explicitly implement Interface members

In order to implement an interface, a class can define an explicit interface member execution body (EXPLICIT interface member implementations). An explicit interface member execution body can be a method, a property, an event, or a definition of an index indicator, and the definition of the discretionary name corresponding to that member should remain consistent.

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-executing bodies.

Description

1. Explicit interface member execution cannot be accessed through full-name in method invocation, property access, and index indicator access. In fact, an explicit interface member execution body 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, nor can it be added abstract, virtual, override, or static modifiers.

3. Explicit interface member execution body and other members have different access methods. Because it cannot be accessed in the method invocation, property access, or index indicator access, the explicit interface member execution body is in some sense private. However, they can be accessed through an instance of the interface, and also have a certain public property.

4, only when the class is defined, the interface name is written in the base class list, and the discretionary, type, and return types defined in the class are fully consistent with the explicit interface member execution body, the explicit interface member execution body is valid, for example:

Class Shape:icloneable {
Object ICloneable.Clone () {...}
int IComparable.CompareTo (object other) {...}
}

The use of explicit interface member actuators typically has two purposes:

1, because the explicit interface member execution body cannot access through the instance of the class, this can separate the implementation part of the interface from the public interface. If a class uses the interface only internally, and the consumer of the class does not directly use the interface, the explicit interface member execution body can play a role.

2. Explicit interface member execution body avoids confusion between interface members because of the same name. If a class wants to implement different implementations of the 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 execution, the class cannot be implemented for interface members with different names and return types.

The following definition is invalid because the shape definition does not appear in the base class list when the interface IComparable.

Class Shape:icloneable
{
Object ICloneable.Clone () {...}
}
Class Ellipse:shape
{
Object ICloneable.Clone () {...}
}

It is wrong to define icloneable.clone in ellipse because ellipse implicitly implements the interface icloneable,icloneable still does not appear explicitly in the ellipse-defined base class list.

The full name of the interface member must correspond to the member defined in the interface. As in the following example, the explicit interface member execution body of Paint must be written as IControl.Paint.

Using System;
Interface IControl
{
void Paint ();
}
Interface Itextbox:icontrol
{
void SetText (string text);
}
Class Textbox:itextbox
{
void IControl.Paint () {...}
void Itextbox.settext (string text) {...}
}

A class that implements an interface can explicitly implement members of that interface. When a member is explicitly implemented, the member cannot be accessed through the class instance, but only through an instance of that interface. An explicit interface implementation also allows programmers to inherit two interfaces that share the same member name and provide a separate implementation for each interface member.

The following example displays the dimensions of the box in both metric and imperial units. The box class inherits IEnglishDimensions and IMetricDimensions two interfaces, which represent different weights and measures systems. Two interfaces have the same member name Length and Width.

Program Listing 1 DemonInterface.cs

Interface IEnglishDimensions {
float Length ();
float Width ();
}
Interface IMetricDimensions {
float Length ();
float Width ();
}
Class Box:ienglishdimensions, IMetricDimensions {
float lengthinches;
float widthinches;
Public Box (float length, float width) {
lengthinches = length;
Widthinches = width;
}
Float Ienglishdimensions.length () {
return lengthinches;
}
Float Ienglishdimensions.width () {
return widthinches;
}
Float Imetricdimensions.length () {
return lengthinches * 2.54F;
}
Float Imetricdimensions.width () {
return widthinches * 2.54F;
}
public static void Main () {
Define a real class object "Mybox"::
Box Mybox = new Box (30.0f, 20.0f);
Define an interface "Edimensions"::
IEnglishDimensions edimensions = (ienglishdimensions) mybox;
IMetricDimensions mdimensions = (imetricdimensions) mybox;
Output:
System.Console.WriteLine ("Length (in): {0}", Edimensions.length ());
System.Console.WriteLine ("Width (in): {0}", Edimensions.width ());
System.Console.WriteLine ("Length (cm): {0}", Mdimensions.length ());
System.Console.WriteLine ("Width (cm): {0}", Mdimensions.width ());
}
}

Output: Length (in): 30,width (in): 20,length (cm): 76.2,width (cm): 50.8

Code discussion: If you want the default metric to be in imperial units, implement both the length and width methods, and explicitly implement the length and width methods from the IMetricDimensions interface:

public float Length () {
return lengthinches;
}
public float Width () {
return widthinches;
}
Float Imetricdimensions.length () {
return lengthinches * 2.54F;
}
Float Imetricdimensions.width () {
return widthinches * 2.54F;
}

In this case, you can access the imperial units from the class instance and access the metric units from the interface instance:

System.Console.WriteLine ("Length (in): {0}", Mybox.length ());
System.Console.WriteLine ("Width (in): {0}", Mybox.width ());
System.Console.WriteLine ("Length (cm): {0}", Mdimensions.length ());
System.Console.WriteLine ("Width (cm): {0}", Mdimensions.width ());

2, Inheritance interface implementation

Interfaces are invariant, but this does not mean that the interface is no longer evolving. Similar to the inheritance of classes, interfaces can also inherit and evolve.

Note: interface inheritance and class inheritance are different, first, class inheritance is not only a description of inheritance, but also an implementation of inheritance, and interface inheritance is simply a description of inheritance. That is, the derived class can inherit the method implementation of the base class, and the derived interface inherits only the member method description of the parent interface, not the implementation of the parent interface, and second, class inheritance in C # allows only single inheritance, but interface inheritance allows multiple inheritance, and a subinterface can have multiple parent interfaces.

Interfaces can inherit from zero or more interfaces. When inheriting from multiple interfaces, use ":" followed by the inherited interface name, with "," split between multiple interface names. The inherited interface should be accessible, such as inheriting from a private type or an interface of type internal, which is not allowed. Interfaces are not allowed to inherit directly or indirectly from themselves. Like inheritance of classes, the inheritance of interfaces also forms a hierarchy between interfaces.

Take a look at the following example:

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 inherits all the members of the interface, and the above example interfaces ITextBox and IListBox inherit from the interface IControl, and inherit the paint method of the interface IControl. The interface IComboBox inherits from the interface ITextBox and IListBox, so it should inherit the SetText method of the interface ITextBox and IListBox method of Setitems, as well as IControl's Paint method.
A class inherits all of the interface implementations that are provided by its base class.

Without explicitly implementing an interface, a derived class cannot change the interface mappings it inherits from its base class in any way. For example, in a declaration

Interface IControl {
void Paint ();
}
Class Control:icontrol {
public void Paint () {...}
}
Class Textbox:control {
New public void Paint () {...}
}

The method in the TextBox paint hides the method paint from the control, but does not alter the mapping from Control.paint to IControl.Paint, while calling Paint through class instances and interface instances will have the following effect

Control C = new control ();
TextBox t = new TextBox ();
IControl IC = c;
IControl it = t;
C.paint (); Influence Control.paint ();
T.paint (); Influence Textbox.paint ();
Ic. Paint (); Influence Control.paint ();
It. Paint (); Influence Control.paint ();

However, when an interface method is mapped to a virtual method in a class, it is impossible for a derived class to overwrite the virtual method and change the implementation function of the interface. For example, re-write the above statement as

Interface IControl {
void Paint ();
}
Class Control:icontrol {
public virtual void Paint () {...}
}
Class Textbox:control {
public override void Paint () {...}
}

You will see the following result:

Control C = new control ();
TextBox t = new TextBox ();
IControl IC = c;
IControl it = t;
C.paint (); Influence Control.paint ();
T.paint (); Influence Textbox.paint ();
Ic. Paint (); Influence Control.paint ();
It. Paint (); Influence Textbox.paint ();

Because an explicit interface member implementation program cannot be declared as virtual, it is not possible to overwrite an explicit interface member implementation program. An explicit interface member implementation invokes another method that is valid, and the other method can be declared virtual so that the derived class can overwrite it. For example:

Interface IControl {
void Paint ();
}
Class Control:icontrol {
void IControl.Paint () {Paintcontrol ();}
protected virtual void Paintcontrol () {...}
}
Class Textbox:control {
protected override void Paintcontrol () {...}
}

Here, classes that inherit from control can specialize the IControl.Paint implementation by overriding the method Paintcontrol.

3. Re-Implement the interface

As we've already covered, derived classes can overload member methods that are already defined in the base class. A similar concept is introduced into the implementation of the class-to-interface, called the interface's re-implementation (re-implementation). Classes that inherit the implementation of an interface can re-implement the interface. This interface requirement is present in the list of base classes defined by the class. The re-implementation of the interface must also strictly adhere to the rules for the first implementation of the interface, which does not have any effect on the interface mappings established for the implementation of the interface.

The following code shows an example of an interface re-implementation:

Interface IControl {
void Paint ();
Class Control:icontrol
void IControl.Paint () {...}
Class Mycontrol:control, IControl
public void Paint () {}
}

The fact is that control maps IControl.Paint to Control.IControl.Paint, but this does not affect the re-implementation in mycontrol. In MyControl implementations, IControl.Paint is mapped to Mycontrol.paint.

When an interface is re-implemented, the inherited public member definition and inherited explicit interface member definitions participate in the process of interface mapping.

Using System;
Interface IMethods {
void F ();
void G ();
void H ();
void I ();
}
Class Base:imethods {
void Imethods.f () {}
void Imethods.g () {}
public void H () {}
public void I () {}
}
Class Derived:base, IMethods {
public void F () {}
void IMethods.H () {}
}

Here, the implementation of interface IMethods in Derived maps the interface method to DERIVED.F,BASE.IMETHODS.G, Derived.imethods.h, and base.i. As we said earlier, when a class implements an interface, it implicitly implements all parent interfaces for that interface. Similarly, when a class is re-implementing an interface, it implicitly implements all parent interfaces for that interface.

Using System;
Interface IBase {
void F ();
}
Interface Iderived:ibase {
void G ();
}
Class C:iderived {
void IBase.F () {
Code for implementation of F ...
}
void Iderived.g () {
Code to implement for G ...
}
}
Class D:c, IDerived {
public void F () {
Code for implementation of F ...
}
public void G () {
Code to implement for G ...
}

}

Here, the re-implementation of the iderived also realized the re-implementation of the IBase, the ibase.f mapped to the D.F.

4. Mapping interface

The class must provide a concrete implementation for the members of all interfaces listed in the base class table. The implementation of locating an interface member in a class is called an interface mapping (interface mapping).

mapping, which mathematically represents the corresponding function relationship of one by one. The same is true of interface mappings, where interfaces are implemented by classes, and for each member defined in the interface, a member of the class should be given a specific implementation.

The following conditions must be met between the members of the class and the interface members that they map:

1. If both A and B are member methods, then A and B names, types, formal parameter lists (including the number of parameters and the type of each parameter) should be consistent.

2. If both A and B are attributes, then the names and types of a and B should be identical, and the accessors of A and b are similar. However, if a is not an explicit interface member performer, a allows to increase its own accessors.

3. If both A and B are time then the names and types of a and B shall be consistent.

4. If both A and B are index indicators, then the type of a and B, the formal parameter list (including the number of parameters and the type of each parameter) should be consistent. And the accessors of A and b are similar. However, if a is not an explicit interface member performer, a allows to increase its own accessors.

So, for an interface member, how do you determine which class member to implement? Which is the member of the class that is mapped by an interface member? Here, we describe the process of interface mapping. Suppose that Class C implements an interface Iinterface,member is a member of the interface IInterface, in which the mapping process of the member is implemented by WHO implements the interface member member:

1, if there is an explicit interface member of the execution body in C, the executor and the interface IInterface and its member member corresponds, it is to implement the member member.

2, if the condition (1) is not sufficient, and there is a non-static public member in C, which corresponds to the interface member member, then it implements the member member.

3. If the above conditions are still not satisfied, look for a C base class D in the list of base classes defined in Class C, substituting D for C.

4. Repeat steps 3, traversing all direct base classes and non-direct base classes of C until a member of the class that satisfies the condition is found.

5. If it is still not found, the error is reported.

The following is an example of invoking a base class method to implement an interface member. Class Class2 implements the interface Interface1, and the members of the class Class2 's base class Class1 also participate in the mapping of the interface, that is, when class Class2 is implemented on the interface Interface1, The Member method F provided by the class Class1 is used to implement the Member method F of the interface Interface1:

Interface Interface1 {
void F ();
}
Class Class1 {
public void F () {}
public void G () {}
}
Class Class2:class1, Interface1 {
New public void G () {}
}

Note: The members of an interface include members of its own definition, and include members of all parent interface definitions for that interface. In an interface map, not only are all members explicitly defined in the interface definition body mapped, but all interface members that are implicitly inherited from the parent interface are mapped.

There are two points to note when you are doing interface mapping:

1. When deciding which member of a class to implement an interface member, an interface member that is explicitly described in a class has precedence over other members.

2. Members that use private, protected, and static modifiers cannot participate in implementing interface mappings. For example:

Interface ICloneable {
Object Clone ();
}
Class C:icloneable {
Object ICloneable.Clone () {...}
public Object Clone () {...}
}

In the example, the member ICloneable.Clone is known as the implementation of the member Clone of the interface icloneable because it is an explicitly described interface member with a higher priority than the other members.

If a class implements two or two interfaces with the same name, type, and parameter types, then one member of the class may implement all of these interface members:

Interface IControl {
void Paint ();
}
Interface Iform {
void Paint ();
}
Class Page:icontrol, Iform {
public void Paint () {...}
}

Here, the method paint for interface IControl and Iform is mapped to the paint method in the class page. Of course, the two methods can be implemented separately with explicit interface members:

Interface IControl {
void Paint ();
}
Interface Iform {
void Paint ();
}
Class Page:icontrol, Iform {
public void IControl.Paint () {
Specific interface Implementation code
}
public void Iform.paint () {
Specific interface Implementation code
}
}

Both of the above are correct. However, if an interface member overrides a member of the parent interface in the inheritance, the implementation of that interface member might have to be mapped to an explicit interface member execution body. Look at the following example:

Interface IBase {
int P {get;}
}
Interface Iderived:ibase {
New int P ();
}

The

Interface iderived inherits from the interface IBase, when the member method of the interface IDerived overrides the member method of the parent interface. Because there are two interface members with the same name, the implementation of the two interface members without explicit interface member execution does not resolve the interface mappings. Therefore, if a class is to implement interface iderived, at least one explicit interface member execution body must be defined in the class. It is reasonable to use the following:
 
//One: Implement
Lass c:iderived {
int ibase.p  for two interface members using explicit interface member-execution bodies;
get 
{//specific interface implementation code}
int iderived.p () {
//specific Interface implementation code}
}
//Two: The interface member of the IBase is implemented with an explicit interface member execution body to implement the
class C:iderived {
int ibase.p
Get {//Concrete Interface Implementation code}
public int P () {
//specific Interface implementation code}
}
//Three: The interface member of the IDerived is implemented with an explicit interface member execution body to implement the
class C:ide rived{
Public int P
Get {//Specific interface implementation code}
int iderived.p () {
//specific Interface implementation code}
}

In another case, if a class implements multiple interfaces that have the same parent interface, the parent interface is only 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 text) {...}
void Ilistbox.setitems (string[] items) {...}
}

In the example above, the class ComboBox implements three interfaces: Icontrol,itextbox and IListBox. If we think that the ComboBox not only realizes the IControl interface, but also realizes ITextBox and IListBox, the parent interface IControl is implemented respectively. In fact, the implementation of interface ITextBox and IListBox is shared, and the docking port icontrol is realized.

We have a comprehensive understanding of the interface of C #, and basically master how to use C # interface programming, but in fact, C # is not only applied to. NET platform, which also supports previous COM, can implement the conversion of COM classes to. NET classes, such as the C # call API. For more information on this, see the next section-Interface transformations.

C # Interface Basics Tutorial Five implementation interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.