尽管已实现的方法具有相同的签名,但代

Bra*_*iat -2 c# interface

我没有意识到为什么这段代码有效?

interface ISumCalculator
{
    int Calc( int x, int y );
}

interface IProductCalculator
{
    int Clac ( int x, int y );
}


class Calculator : ISumCalculator, IProductCalculator
{

    public int Calc(int x, int y)
    {
        throw new NotImplementedException();
    }

    public int Clac(int x, int y)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

Tig*_*ran 8

这些方法具有相同的签名,但名称不同,因此这里没有问题.

即使在名称和签名完全相同的情况下,您也可以通过在方法定义中明确定义接口来轻松克服问题:

class Calculator : ISumCalculator, IProductCalculator
{

    int IProductCalculator.Calc(int x, int y)
    {
        throw new NotImplementedException();
    }

    int ISumCalculator.Calc(int x, int y)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)