(JavaFX) 如何在构造函数和initialize() 之间调用class 的方法?

(JavaFX) How to call method of class between constructor and initialize()?

我有一个实现接口的 class。从我的基础 class,我正在创建一个已实现 class 的实例,如下所示:

Class newTab;
TransactionTabInterface tabInterface = null;
try {
  newTab = Class.forName("[package] + title);
  tabInterface = (TransactionTabInterface) newTab.newInstance();
  tabInterface.setRootController(this);
} catch (Exception e) {
   e.printStackTrace();
}

这是必要的,因为我不知道我需要实例化的 class 的名称(title 在别处确定)。

我遇到的问题是,在新 class 中,我需要立即访问 rootController。我在 class 中有一个方法允许我传递 rootController 引用,如我上面的代码所示。

通常情况下,我会在实例化class时将rootController传递给构造函数,但我不知道如何在上面的newInstance()调用中将参数传递给构造函数.

编辑:要清楚,上面的代码在我的主要class中,用于实例化一个新的FXML控制器(newTab)。

在您的 newTab 对象上,您可以使用构造函数的类型调用 getConstructor(Class<?>...) 以获取对具有所需参数的构造函数的引用。

然后就可以调用这个构造函数的newInstance(Object...)方法来调用带参数的构造函数了