Implement interface 2

xiaoxiao2021-03-06  58

There are two purposes that use explicit interface members to actually have two purposes:

1. Because the explicit interface member actuator cannot access the class's instance, this can separate from the realization portion of the interface from the public interface. If a class is only used inside, the user does not directly use the interface, and this explicit interface member executive can play a role.

2. The explicit interface member executive avoids confusion between interface members because of the same name. If a class wants to use different implementations of interface members with the same name and return type, this must be used to use the explicit interface member executive. If there is no explicit interface member executive, the class is not implemented for the interface members of the name and return type.

The following definition is invalid, because the interface iComparable is displayed in the base class list when the Shape definition.

Class Shape: Icloneable {Object iCloneable.clone () {...}} Class Ellipse: Shape {Object IcloneAble.clone () {...}}

Defining icloneable.clone in Ellipse is incorrect because Ellipse is implicitly implemented interface iCloneable, iCloneable still does not explicitly appear in the list of Ellipse defined base clauses.

The full name of the interface member must correspond to the members defined in the interface. As in the following example, the Paint's explicit interface member actuator must be written into 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) {...} }

The class that implements the interface can explicitly implement the member of the interface. When a member is explicitly implemented, the member cannot be accessed by a class instance, and the member can only be accessed by an instance of the interface. Explicit interface implementation also allows programmers to inherit two interfaces of sharing the same member name and provide a separate implementation for each interface member.

The size of the box is simultaneously displayed in the metric unit and the inch unit in the following example. The Box class inherits the two interfaces of IenglishDimensions and IMEtricDimensions, which represent different degree balance systems. The two interfaces have the same member name Length and Width.

Program List 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 WidthIns * 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: {0}", edimensions.width ()); System.Console.writeline ("Length (cm): {0}", mdimensions.Length ()); System.Console.wri "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 metrics to use English units, you will implement both methods of leadth and width, and explicitly implement the Length and Width methods from the iMtricDimensions 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, the English unit can be accessed from the class instance, and the metric unit is accessed from the interface instance:

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

New Post(0)