您的位置:首页 > 汽车 > 新车 > 搭建系统_黑马程序员python教程_免费长尾词挖掘工具_个人主页网页设计模板

搭建系统_黑马程序员python教程_免费长尾词挖掘工具_个人主页网页设计模板

2025/4/9 2:42:39 来源:https://blog.csdn.net/m0_72813396/article/details/144572486  浏览:    关键词:搭建系统_黑马程序员python教程_免费长尾词挖掘工具_个人主页网页设计模板
搭建系统_黑马程序员python教程_免费长尾词挖掘工具_个人主页网页设计模板

显式接口实现还允许程序员实现具有相同成员名称的两个接口,并为每个接口成员各提供一个单独的实现。 本示例同时以公制单位和英制单位显示框的尺寸。 Box 类实现 IEnglishDimensions 和 IMetricDimensions 两个接口,它们表示不同的度量系统。 两个接口有相同的成员名称 Length 和 Width。

示例
// Declare the English units interface:
interface IEnglishDimensions
{float Length();float Width();
}// Declare the metric units interface:
interface IMetricDimensions
{float Length();float Width();
}// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{float lengthInches;float widthInches;public Box(float lengthInches, float widthInches){this.lengthInches = lengthInches;this.widthInches = widthInches;}// 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;static void Main(){// Declare a class instance box1:Box box1 = new Box(30.0f, 20.0f);// Declare an instance of the English units interface:IEnglishDimensions eDimensions = box1;// Declare an instance of the metric units interface:IMetricDimensions mDimensions = box1;// Print dimensions in English units:System.Console.WriteLine("Length(in): {0}", eDimensions.Length());System.Console.WriteLine("Width (in): {0}", eDimensions.Width());// Print dimensions in metric units:System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());}
}
/* Output:Length(in): 30Width (in): 20Length(cm): 76.2Width (cm): 50.8
*/
可靠编程

如果希望默认度量采用英制单位,请正常实现 Length 和 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;

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

public static void Test()
{Box box1 = new Box(30.0f, 20.0f);IMetricDimensions mDimensions = box1;System.Console.WriteLine("Length(in): {0}", box1.Length());System.Console.WriteLine("Width (in): {0}", box1.Width());System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com