我有一个类,其构造函数如下所示:
abstract class BasePanel extends JPanel {
public BasePanel(A a) {
// initializing fields from values passed to ctor
this.a = a;
// initializing gui components
initializeComponents();
setupPanels();
concludeUiSetup();
}
// stuff
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中,首先初始化要使用传递给构造函数的值进行初始化的字段.然后按顺序调用UI设置所需的其他方法.需要在子类中重写其中两个方法,以便针对它们进行UI设置.
现在考虑一个FooPanel扩展的类BasePanel.它在构造函数中需要更多的初始化参数.
class FooPanel extends BasePanel {
public FooPanel(A a, B b) {
super(a);
this.b = b;
}
@Override
public void initializeComponents() {
super.initializeComponents();
// I require b here, but oops, b is not initialized at this point, and so
// this will …Run Code Online (Sandbox Code Playgroud)