我有抽象课:
public abstract class MyComposite extends Composite {
protected abstract void initHandlers();
}
还有一大堆扩展它的类。如何确保在子类构造结束时调用initHandlers()方法?示例子类:
public CollWidget extends MyComposite {
public CollWidget() {
/** Some stuff thats need to be done in this particular case */
initHandlers(); // This method should be invoked transparently
}
@Override
public void initHandlers() {
/** Handlers initialisation, using some components initialized in constructor */
}
}
答案 0 :(得分:1)
没有办法自动完成,因为父构造函数总是在子构造函数之前被调用(显式或隐含)。
一个解决方案解决方法可能是:
public abstract class MyComposite {
public MyComposite() {
construct();
initHandlers();
}
protected abstract void construct();
protected abstract void initHandlers();
}
public class CollWidget extends MyComposite {
@Override
protected void construct() {
// called firstly
}
@Override
public void initHandlers() {
// called secondly
}
}