Java 我可以使用类对象实例化一个类吗?构造函数呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/712371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:38:25  来源:igfitidea点击:

Can I Instantiate a class using the class object? What about Constructors?

javareflection

提问by

I am storing a list of classes through (Classname.class) and would like to instantiate one? Is this possible?

我正在通过 ( Classname.class)存储一个类列表并想实例化一个?这可能吗?

newInstanceseems to the method I am after but it doesn't support a constructor?

newInstance似乎我所追求的方法但它不支持构造函数?

采纳答案by Peter Lawrey

You cannot construct new classes this way.

您不能以这种方式构造新类。

If you have the name of a class you can use Class.forName(className) to load/reference a class.

如果你有一个类的名称,你可以使用 Class.forName(className) 来加载/引用一个类。

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.

如果您有要创建的类的字节码,则可以让类加载器加载字节码并为您提供类。这可能比您预期的更先进。

回答by andri

You can use Class.getConstructors(or Class.getConstructor) to get a list of available constructors, and invoke any of them with Constructor.newInstance, which does accept parameters.

您可以使用Class.getConstructors(或 Class.getConstructor)来获取可用构造函数的列表,并使用Constructor.newInstance调用它们中的任何一个,它接受参数。

回答by deverton

The Java tutorial on reflectioncovers this well. But yeah, basically Class.getConstructors, then Constructor.newInstance is where it's at.

关于反射的 Java教程很好地涵盖了这一点。但是是的,基本上是 Class.getConstructors,然后是 Constructor.newInstance。

回答by Tom Hawtin - tackline

If you have a list of Classobjects obtained through class literals, you might as well statically reference the constructors rather than slipping into reflection evilness.

如果你有一个Class通过类字面量获得的对象列表,你最好静态引用构造函数,而不是陷入反射的邪恶。

回答by Hugo

Java is designed so you can never "trick" it as long as you use the java.lang/java. classes or other standard libraries. One of the most important things of OOP is that objects should be in a defined state, thus you can be safe that the constructor is always run. Even if you're using some strange-looking reflection libraries to get your work done.

Java 被设计成只要你使用 java.lang/java,你就永远不会“欺骗”它。类或其他标准库。OOP 最重要的事情之一是对象应该处于定义的状态,因此您可以安全地始终运行构造函数。即使您正在使用一些看起来很奇怪的反射库来完成您的工作。

So, using Class.forName("me.Test").newInstance(); (or similar) will under-the-hood invoke the Test() constructor for you.

所以,使用 Class.forName("me.Test").newInstance(); (或类似的)将在后台为您调用 Test() 构造函数。

If you want to invoke another constructor the code is something like:

如果要调用另一个构造函数,代码类似于:

Test test = (Test)Class.forName("Test").getConstructor(String.class).newInstance("Hello World");

Here the getConstructor asks what the constructor looks like (it wants a string) and then you call it with a string.

这里 getConstructor 询问构造函数是什么样子(它需要一个字符串),然后你用一个字符串调用它。

回答by dstine

Just to add one point I see missing:

只是补充一点,我认为缺少:

You can invoke newInstancedirectly on the Classobject if it has a public null constructor. (Null constructor is the constructor with no arguments.)

如果对象具有公共空构造函数,则可以newInstance直接在该Class对象上调用。(空构造函数是没有参数的构造函数。)

Otherwise, you can find constructors via Class.getConstructors()as others have said.

否则,您可以通过Class.getConstructors()其他人所说的方式找到构造函数。