相关疑难解决方法(0)

如何在特定情况下处理变量的初始化

我有一个类,其构造函数如下所示:

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)

java oop swing refactoring initialization

1
推荐指数
2
解决办法
160
查看次数

标签 统计

initialization ×1

java ×1

oop ×1

refactoring ×1

swing ×1