【问题标题】:Cannot understand interface inheritance无法理解接口继承
【发布时间】:2017-01-01 11:18:49
【问题描述】:

下面的代码给了我错误

错误 2 'Series.AB' 类型已经包含 'B' 的定义

 interface IA
{
    void A();
}
interface IB
{
    void B();
}

interface IAB : IA, IB
{
    void A();
    void B();
}

class AB : IAB
{
    IA A;
    IB B;
    public AB(IA _a, IB _b)
    {
        A = _a;
        B = _b;
    }

    public void A()
    {
        throw new NotImplementedException();
    }

    public void B()
    {
        throw new NotImplementedException();
    }
}

我认为我可以使用 AB 类实例作为

IA A = 新 AB();或 IB B = 新 AB();或 IAB ab = new AB();

我无法理解这里发生了什么。请任何人描述为什么会发生此异常。

【问题讨论】:

标签: c#


【解决方案1】:

将属性名称更改为 A 和 B 以外的其他名称,因为您的方法名称具有相同的名称。在 AB 类中也有默认构造函数。

class AB : IAB
{
    IA instanceA;
    IB instanceB;
    public AB(IA _a, IB _b)
    {
        instanceA = _a;
        instanceB = _b;
    }

    public void A()
    {
        throw new NotImplementedException();
    }

    public void B()
    {
        throw new NotImplementedException();
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2021-04-10
    相关资源
    最近更新 更多