问候和致敬!
我目前有一个抽象类A,许多类继承它.我在oneMethod()中放入的所有子类共有的代码,以及特定于每个实现的代码,我将它放入两个抽象方法中.
public abstract class AbstractA {
public oneMethod() {
//do some intelligent stuff here
abstractMethodOne();
abstractMethodTwo();
}
protected abstract void abstractMethodOne();
protected abstract void abstractMethodTwo();
}
Run Code Online (Sandbox Code Playgroud)
我有一个类覆盖了oneMethod()方法.
public class B extends AbstractA {
@Override
public oneMethod() {
//do some other intelligent stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法跳过子类中的两个抽象方法的存根实现?我的意思是他们使用的唯一地方是被覆盖的方法.
任何帮助表示赞赏!
否.如果扩展抽象类,则必须使子类为抽象类,或者必须满足父类的约定.
作为设计观察,我建议您尝试将oneMethod()设为final或abstract.很难维护允许扩展的程序,就像你实现它一样.使用其他抽象方法将子类挂钩到oneMethod()的功能中.