-2

このコードが機能する理由がわかりませんでした。

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();
    }
}
4

2 に答える 2

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();
    }
}
于 2013-11-04T15:30:38.363 に答える
1

Calc()Clac()は 2 つの異なるメソッド名です。

于 2013-11-04T15:31:36.037 に答える