问题描述
我通过( Classname.class
)存储类的列表,并希望实例化一个类?这可能吗?
I am storing a list of classes through (Classname.class
) and would like to instantiate one? Is this possible?
newInstance
似乎是我所使用的方法,但它不支持构造函数?
newInstance
seems to the method I am after but it doesn't support a constructor?
推荐答案
你不能用这种方式构建新类。
You cannot construct new classes this way.
如果你有您可以使用Class.forName(className)来加载/引用类的类的名称。
If you have the name of a class you can use Class.forName(className) to load/reference a class.
如果您有要创建的类的字节代码可以让类加载器加载字节代码并为您提供类。这可能比你想象的更先进。
If you have the byte code for a class you want to create you can have a class loader load the byte code and give you the class. This is likely to be more advanced than you intended.
这篇关于我可以使用类对象实例化一个类吗?那些构造函数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!