覆盖继承的抽象类中的抽象方法

the*_*tor 8 c# inheritance abstract-class abstract-methods multi-level

好吧基本上我有以下问题:我试图让一个抽象类继承另一个抽象方法,但是我不想在其中任何一个中实现抽象方法,因为第三个类继承了他们:

public abstract class Command
{
      public abstract object execute();
}

public abstract class Binary : Command
{
     public abstract object execute(); //the issue is here
}

public class Multiply : Binary
{
     public override object execute()
     {
           //do stuff
     }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将二进制命令与一元命令分开,但不希望/不能实现任何一种执行方法.我想过让Binary覆盖抽象方法(因为它必须),然后只抛出一个未实现的异常事物.如果我覆盖它,那么我必须声明一个体,但如果我把它抽象,那么我就是"隐藏"继承的方法.

有什么想法吗?

Mik*_*ill 15

您不需要execute()在Binary类中声明,因为它已经从Command继承.抽象方法不需要由其他抽象类实现 - 需求被传递给最终的具体类.

public abstract class Command
{
    public abstract object execute();
}

public abstract class Binary : Command
{
    //the execute object is inherited from the command class.
}

public class Multiply : Binary
{
    public override object execute()
    {
        //do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)