显式实现接口(Implement Interface explicitly)

本文通过一个具体的案例展示了如何在一个类中实现多个具有相同成员名称的接口,并通过显式接口实现来区分这些成员的方法。此类技术常用于.NET等面向对象编程环境中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当需要实现具有相同成员名称的不同接口时,采用显式接口实现.
interface IEnglishDimensions
    {
        float Length();
        float Width();
    }

    // Declare the metric units interface:
    interface IMetricDimensions
    {
        float Length();
        float Width();
    }

    public class test : IEnglishDimensions, IMetricDimensions
    {
        float _width;
        float _height;
        public test(float width, float height)
        {
            _width = width;
            _height = height;
        }

        #region IEnglishDimensions Members

        public float Length()
        {
            return _height;
        }

        public float Width()
        {
            return _width;
        }

        #endregion

        #region IMetricDimensions Members

        float IMetricDimensions.Length()
        {
            return _height * 2f;
        }

        float IMetricDimensions.Width()
        {
            return _width * 2f;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值