简体   繁体   English

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

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

I have a class that implements an interface. 我有一个实现接口的类。 From my base class, I'm creating an instance of the implemented class like this: 从我的基类中,我正在创建实现类的实例,如下所示:

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

This is necessary since I don't know the name of the class I need to instantiate ( title is determined elsewhere). 这是必需的,因为我不知道需要实例化的类的名称( title在其他位置确定)。

The problem I'm having is that, within the new class, I need access to rootController immediately. 我遇到的问题是,在新类中,我需要立即访问rootController I have a method within the class that allows me to pass the rootController reference, as seen in my above code. 我在类中有一个方法,可以让我传递rootController引用,如上面的代码所示。

Normally, I would pass rootController to the constructor when instantiating the class, but I don't know how to pass a parameter to the constructor in the newInstance() call above. 通常,在实例化类时,我会将rootController传递给构造函数,但是我不知道如何在上面的newInstance()调用中将参数传递给构造函数。

EDIT : To be clear, the code above is in my main class, used to instantiate a new FXML controller (newTab). 编辑 :显然,上面的代码在我的主类中,用于实例化新的FXML控制器(newTab)。

on your newTab object you can call getConstructor(Class<?>...) with the types of your constructor to get a reference to that constructor with the desired args. newTab对象上,可以使用构造函数的类型调用getConstructor(Class<?>...) ,以获取具有所需args的对该构造函数的引用。

Then you can call this constructor's newInstance(Object...) method to call the constructor with arguments. 然后,您可以调用此构造函数的newInstance(Object...)方法来调用带有参数的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM