-1

我尝试了以下,它工作正常:

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;
    }
}

Bu尝试这样做时没有用:

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 派生一个类并实现所有三种方法。

4

2 回答 2

0

我怀疑魔鬼在细节中,但是要编译您的示例,您的AbsSum2类只需要overrideSum (来自AbsSum

abstract class AbsSum2 : AbsSum1, ISum
{
    public override int Sum(int x, int y)
    {
        return x + y;
    }
} 
于 2013-11-04T16:30:14.527 回答
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);
 }

}

于 2013-11-04T16:34:04.990 回答