当前位置: 首页 > article >正文

windows C#-显式实现两个接口的成员

显式接口实现还允许程序员实现具有相同成员名称的两个接口,并为每个接口成员各提供一个单独的实现。 本示例同时以公制单位和英制单位显示框的尺寸。 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): 30
    Width (in): 20
    Length(cm): 76.2
    Width (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());
}

http://www.kler.cn/a/458641.html

相关文章:

  • pytorch张量的fill_方法介绍
  • hhdb客户端介绍(64)
  • 【MySQL】触发器
  • leetcode 面试经典 150 题:矩阵置零
  • 【openGauss】正则表达式次数符号“{}“在ORACLE和openGauss中的差异
  • mac如何查看使用git克隆下来的文件.git 文件【收藏版】
  • 单元测试/系统测试/集成测试知识总结
  • 钉钉h5微应用安卓报错error29 ios报错error3 加上报错52013,签名校验失败 (前端)
  • kubernetes Gateway API-1-部署和基础配置
  • redis——岁月云实战
  • 代码随想录Day57 prim算法精讲,kruskal算法精讲。
  • 洪水模拟示例代码
  • openEuler安装OpenGauss5.0
  • HTML-CSS(day01)
  • 佛塔宝珠c++
  • 解锁自动化新高度,zTasker v2.0全方位提升效率
  • NoETL 自动化指标平台如何保障数据质量和口径一致性?
  • 车载软件架构 --- Autosar OS
  • IOS 关于ARKi使用
  • 通过Cephadm工具搭建Ceph分布式存储以及通过文件系统形式进行挂载的步骤