[英]overriding abstract methods in an inherited abstract class
好吧基本上我有以下问题:我试图让一个抽象类继承另一个抽象方法,但是我不想在其中任何一个中实现抽象方法,因为第三个类继承了他们:
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
}
}
我正在尝试将二进制命令与一元命令分开,但不希望/不能实现任何一种执行方法。 我想过让Binary覆盖抽象方法(因为它必须),然后只抛出一个未实现的异常事物。 如果我覆盖它,那么我必须声明一个体,但如果我把它抽象,那么我就是“隐藏”继承的方法。
有什么想法吗?
您不需要在Binary类中声明execute()
,因为它已经从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
}
}
根本不要在Binary
中省略execute()
的声明。 由于Binary
也是抽象的,因此您不必实现其祖先的任何抽象方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.