我尝试了以下内容并且工作正常:
interface ICalculator
{
int CalculateSum(int x, int y);
int CalculateProduct(int x, int y);
}
abstract class AbstractCalculator : ICalculator
{
public abstract int CalculateSum(int x, int y);
public int CalculateProduct(int x, int y)
{
return x * y;
}
}
尝试这个时它不起作用:
interface ISum
{
int Sum ( int x, int y );
}
abstract class AbsSum1
{
public abstract int Sum ( int x, int y );
}
abstract class AbsSum2 : AbsSum1, ISum
{
public abstract int AbsSum1.Sum ( int x, int y );
public abstract int ISum.Sum ( int x, int y );
public abstract int Sum ( int x, int y);
}
任何人都可以帮助我吗? 我只是测试它是否可以在c#中完成。我希望从AbsSum2派生一个类并实现所有三种方法。
答案 0 :(得分:0)
我怀疑魔鬼在细节上,但为了让你的例子进行编译,你的AbsSum2
课只需要override
总和(来自AbsSum
:
abstract class AbsSum2 : AbsSum1, ISum
{
public override int Sum(int x, int y)
{
return x + y;
}
}
答案 1 :(得分:0)
当你实现一个界面时,你几乎有两个选择
1)隐式实施:
abstract class AbsSum2 : AbsSum1, ISum
{
// Since the base class already has a **public** Sum() method with proper signature
// ISum will use the method declared in the base class.
}
2)明确地做:
abstract class AbsSum2 : AbsSum1, ISum
{
// Manually redirect interface to Sum() method declared in the base class.
int ISum.Sum(int x, int y)
{
return this.Sum(x, y);
}
}