【问题标题】:if two interface having same method name that time how we achieve multiple inheritance in C#如果两个接口具有相同的方法名称,那么我们如何在 C# 中实现多重继承
【发布时间】:2020-04-09 13:55:04
【问题描述】:
interface A {
    void method();
}

interface B {
    void method();
}

class c : A,B {
//answer
}

【问题讨论】:

  • 我们无法在 C# 中实现多重继承。实现多个接口不是多重继承。

标签: c# oop inheritance interface


【解决方案1】:

您可以显式地实现接口。

class C : A, B
{
    void A.Method()
    {
        // explicit implementation of interface A
    }

    void B.Method()
    {
        // explicit implementation of interface B
    }

    public void Method()
    {
        // class implementation
    }
}

您应该知道如何调用特定方法。

var c = new C();

c.Method();       // class implementation
((B)c).Method();  // implementation of B
((A)c).Method();  // implementation of A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2015-03-11
    • 2022-01-22
    • 2020-12-09
    • 2020-12-05
    • 2011-07-26
    相关资源
    最近更新 更多