我没有意识到为什么这段代码会起作用?
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();
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分:1)
Calc()
和Clac()
是两种不同的方法名称。