简体   繁体   English

C#中的接口的钻石问题是否可能?

[英]Is diamond problem with interfaces in C# possible?

Are there architecture problems in the code below? 下面的代码中是否存在架构问题? Is the so called diamond problem possible with interfaces or similar issues? 接口或类似问题是否可能出现所谓的钻石问题

interface IComponent
{
    void DoStuff();
}

interface ITitledComponent : IComponent
{
    string Title { get; }
}

abstract class ComponentBase : IComponent
{
    public void DoStuff()
    {
        throw new NotImplementedException();
    }
}

class MyComponent : ComponentBase, ITitledComponent
{
    public string Title => throw new NotImplementedException();
}

Certainly, diamond inheritance with classes is a bad decision and this is not possible in C#. 当然,钻石继承与类是一个错误的决定,这在C#中是不可能的。 But about the interfaces I did not find information. 但关于接口我没有找到信息。

No, it's not possible to produce a diamond problem with C#, because you can only ever inherit from one class. 不,用C#生成钻石问题是不可能的,因为你只能从一个类继承。 Interfaces are not inherited, but implemented. 接口不是继承的,而是实现的。 So the actual problem for the compiler and coder, having two implementations of a method and not knowing which to pick for a specific class can never happen. 因此编译器和编码器的实际问题是,具有两个方法的实现并且不知道选择哪个特定类,这种情况永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM