显式实现接口:C# 显式接口实现教程



本教程演示如何显式实现接口成员以及如何从接口例子访问这些成员

=dtH2>教程

实现接口类可以显式实现该接口成员当显式实现某成员时不能通过类例子访问该成员而只能通过该接口例子访问该成员本教程包含两个举例个举例阐释如何显式实现和访问接口成员第 2个举例展示如何实现具有相同成员名两个接口

=dtH4>举例 1

本举例声明=ce>IDimensions 接口和=ce>Box该类显式实现接口成员 =ce>Length=ce>Width通过接口例子 =ce>myDimensions 访问这些成员

// explicit1.cserface IDimensions { float Length; float Width;} Box : IDimensions { float lengthInches; float widthInches; public Box(float length, float width) { lengthInches = length; widthInches = width; } // Explicit erface member implementation: float IDimensions.Length { lengthInches; } // Explicit erface member implementation: float IDimensions.Width { widthInches; } public void Main { // Declare a instance \"myBox\": Box myBox = Box(30.0f, 20.0f); // Declare an erface instance \"myDimensions\": IDimensions myDimensions = (IDimensions) myBox; // Pr out the dimensions of the box: /* The following commented lines would produce compilation errors because they try to access an explicitly implemented erface member from a instance: */ //.Console.WriteLine(\"Length: {0}\", myBox.Length); //.Console.WriteLine(\"Width: {0}\", myBox.Width); /* Pr out the dimensions of the box by calling the methods from an instance of the erface: */ .Console.WriteLine(\"Length: {0}\", myDimensions.Length); .Console.WriteLine(\"Width: {0}\", myDimensions.Width); }}

=dtH4>输出

Length: 30Width: 20

=dtH4>代码讨论

  • 请注意 Main 思路方法中下列代码行被注释掉它们将产生编译显式实现接口成员不能从类例子访问: //.Console.WriteLine(\"Length: {0}\", myBox.Length);//.Console.WriteLine(\"Width: {0}\", myBox.Width);

  • 还请注意Main 思路方法中下列代码行成功输出框尺寸这些思路方法是从接口例子: .Console.WriteLine(\"Length: {0}\", myDimensions.Length);.Console.WriteLine(\"Width: {0}\", myDimensions.Width);

=dtH4>举例 2

显式接口实现还允许员继承共享相同成员名两个接口并为每个接口成员提供个单独实现本举例同时以公制单位和英制单位显示框尺寸=ce>Box 类继承 =ce>IEnglishDimensions=ce>IMetricDimensions 两个接口它们表示区别度量衡系统两个

接口有相同成员名 =ce>Length=ce>Width

// explicit2.cs// Declare the English units erface:erface IEnglishDimensions { float Length; float Width;}// Declare the metric units erface:erface IMetricDimensions { float Length; float Width;}// Declare the \"Box\" that implements the two erfaces:// IEnglishDimensions and IMetricDimensions: Box : IEnglishDimensions, IMetricDimensions { float lengthInches; float widthInches; public Box(float length, float width) { lengthInches = length; widthInches = width; }// Explicitly implement the members of IEnglishDimensions: float IEnglishDimensions.Length { lengthInches; } float IEnglishDimensions.Width { widthInches; }// Explicitly implement the members of IMetricDimensions: float IMetricDimensions.Length { lengthInches * 2.54f; } float IMetricDimensions.Width { widthInches * 2.54f; } public void Main { // Declare a instance \"myBox\": Box myBox = Box(30.0f, 20.0f); // Declare an instance of the English units erface: IEnglishDimensions eDimensions = (IEnglishDimensions) myBox; // Declare an instance of the metric units erface: IMetricDimensions mDimensions = (IMetricDimensions) myBox; // Pr dimensions in English units: .Console.WriteLine(\"Length(in): {0}\", eDimensions.Length); .Console.WriteLine(\"Width (in): {0}\", eDimensions.Width); // Pr dimensions in metric units: .Console.WriteLine(\"Length(cm): {0}\", mDimensions.Length); .Console.WriteLine(\"Width (cm): {0}\", mDimensions.Width); }}

=dtH4>输出

Length(in): 30Width (in): 20Length(cm): 76.2Width (cm): 50.8

=dtH4>代码讨论

如果希望默认度量采用英制单位请正常实现 =ce>Length=ce>Width 这两个思路方法并从 IMetricDimensions 接口显式实现 Length 和 Width 思路方法:

// Normal implementation:public float Length{ lengthInches;}public float Width{ widthInches;}// Explicit implementation:float IMetricDimensions.Length { lengthInches * 2.54f;}float IMetricDimensions.Width { widthInches * 2.54f;}

这种情况下可以从类例子访问英制单位而从接口例子访问公制单位:

.Console.WriteLine(\"Length(in): {0}\", myBox.Length);.Console.WriteLine(\"Width (in): {0}\", myBox.Width); .Console.WriteLine(\"Length(cm): {0}\", mDimensions.Length);.Console.WriteLine(\"Width (cm): {0}\", mDimensions.Width);


Tags:  接口的实现 c语言接口与实现 显式实现接口

延伸阅读

最新评论

发表评论