Java:扩展 class 并添加无参数构造函数(用于包装器)
Java: Extending a class and adding a no-arg constructor (for wrapper)
我需要扩展 class ChartViewer,它没有无参数构造函数,带有 class ChartViewerWrapper 添加无参数构造函数。
我该怎么做?
额外信息(不相关)
我需要这样做的原因是作为一种解决方法,如下所示,引用来自一个关于 JavaFX 8 和 JFreeChart 的论坛。我想在 FXML 中使用 ChartViewer:
Currently the ChartViewer node cannot be used in FXML. I think the
only reason is because it does not have a constructor without
arguments (when I extended the ChartViewer class and added a simple
constructor without arguments, I could add my wrapper class into FXML
and it works).
(我可以直接在 ChartViewer 中添加一个无参数构造函数,但它是一个外部库,出于各种原因,我决定不想这样做.)
在 ChartViewerWrapper
中提供无参数构造函数,并在其第一行调用
super (arg1, arg2);
假设ChartViewer
有两个参数
- 抱歉,我不知道
ChartViewer
的真正构造函数是什么样的
虽然 Scary Wombat 回答了我的一般问题,但这是针对我的特殊情况的解决方案:
public class ChartViewerWrapper extends ChartViewer {
public ChartViewerWrapper() {
super(new JFreeChart(new XYPlot())); // just an empty placeholder...
}
}
然后你可以在 FXML 中使用 ChartViewerWrapper,例如像这样:
<Pane>
<ChartViewerWrapper />
</Pane>
并在需要时更改控制器中的关联属性。
我需要扩展 class ChartViewer,它没有无参数构造函数,带有 class ChartViewerWrapper 添加无参数构造函数。
我该怎么做?
额外信息(不相关)
我需要这样做的原因是作为一种解决方法,如下所示,引用来自一个关于 JavaFX 8 和 JFreeChart 的论坛。我想在 FXML 中使用 ChartViewer:
Currently the ChartViewer node cannot be used in FXML. I think the only reason is because it does not have a constructor without arguments (when I extended the ChartViewer class and added a simple constructor without arguments, I could add my wrapper class into FXML and it works).
(我可以直接在 ChartViewer 中添加一个无参数构造函数,但它是一个外部库,出于各种原因,我决定不想这样做.)
在 ChartViewerWrapper
中提供无参数构造函数,并在其第一行调用
super (arg1, arg2);
假设ChartViewer
有两个参数
- 抱歉,我不知道
ChartViewer
的真正构造函数是什么样的
虽然 Scary Wombat 回答了我的一般问题,但这是针对我的特殊情况的解决方案:
public class ChartViewerWrapper extends ChartViewer {
public ChartViewerWrapper() {
super(new JFreeChart(new XYPlot())); // just an empty placeholder...
}
}
然后你可以在 FXML 中使用 ChartViewerWrapper,例如像这样:
<Pane>
<ChartViewerWrapper />
</Pane>
并在需要时更改控制器中的关联属性。