java字符串到类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4030618/
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
java String to class
提问by MANU SINHA
I have got a bean class named Bean1
. In my main method I have got a string containing the name of the variable:
我有一个名为Bean1
. 在我的主要方法中,我有一个包含变量名称的字符串:
String str= "Bean1";
Now how can I use the String
variable to get the class and access the Bean properties?
现在如何使用String
变量来获取类并访问 Bean 属性?
回答by Kel
You should use Java Reflection API:
您应该使用 Java 反射 API:
Class c = Class.forName("package.name.Bean1");
Then you may use c.newInstance()to instantiate your class. This method uses constructor which does not require parameters.
然后你可以使用c.newInstance()来实例化你的类。此方法使用不需要参数的构造函数。
See details here: http://download.oracle.com/javase/tutorial/reflect/
在此处查看详细信息:http: //download.oracle.com/javase/tutorial/reflect/
回答by Nick
Duplicate of Does Java support variable variables?
Java doesn't support dynamically getting a variable based on a string of its name (also known as variable variables). There's likely a different way of doing what you're trying to do, such as using a Map object to map names to beans. If you edit your question to explain what you want to do in a bit more detail, we might have some more concrete answers.
Java 不支持根据名称的字符串动态获取变量(也称为变量变量)。可能有一种不同的方式来做您想做的事情,例如使用 Map 对象将名称映射到 bean。如果您编辑问题以更详细地解释您想要做什么,我们可能会提供一些更具体的答案。
(On the other hand, if the question was about a class called Bean1, then Kel's right.)
(另一方面,如果问题是关于一个名为 Bean1 的类,那么 Kel 是对的。)
回答by Tomas Narros
Step by step:
一步步:
//1. As Kel has told you (+1), you need to use
//Java reflection to get the Class Object.
Class c = Class.forName("package.name.Bean1");
//2. Then, you can create a new instance of the bean.
//Assuming your Bean1 class has an empty public constructor:
Object o = c.newInstance();
//3. To access the object properties, you need to cast your object to a variable
// of the type you need to access
Bean1 b = (Bean1) o;
//4. Access the properties:
b.setValue1("aValue");
For this last step, you need to know the type of the bean, or a supertype with the properties you need to access. And I guess that you don't know it, if all the information you have on the class is a String with its name.
对于最后一步,您需要知道 bean 的类型,或具有您需要访问的属性的超类型。我猜你不知道,如果你在这个类上拥有的所有信息都是一个带有它的名字的字符串。
Using reflection, you could access the methods of the class, but in this case, you would need to know the names and the input parameter types of the methods to be invoked. Going ahead with the example, change the steps 3 and 4:
使用反射,您可以访问类的方法,但在这种情况下,您需要知道要调用的方法的名称和输入参数类型。继续该示例,更改步骤 3 和 4:
// 3. Get the method "setValue1" to access the property value1,
//which accepts one parameter, of String type:
Method m=c.getMethod("setValue1", String.class);
// 4. Invoke the method on object o, passing the String "newValue" as argument:
m.invoke(o, "newValue");
Maybe you need to rethink your design, if you don't have all this information avalaible at runtime.
如果您没有在运行时可用的所有这些信息,也许您需要重新考虑您的设计。