java 字符串到类

发布于 2024-09-28 21:19:20 字数 161 浏览 4 评论 0原文

我有一个名为 Bean1 的 bean 类。在我的 main 方法中,我得到了一个包含变量名称的字符串:

String str= "Bean1"; 

现在如何使用 String 变量来获取类并访问 Bean 属性?

I have got a bean class named Bean1. In my main method I have got a string containing the name of the variable:

String str= "Bean1"; 

Now how can I use the String variable to get the class and access the Bean properties?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-10-05 21:19:20

一步一步:

//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");

对于最后一步,您需要知道 bean 的类型,或者具有您需要访问的属性的超类型。我猜你不知道,如果你拥有的关于类的所有信息都是一个带有其名称的字符串。

使用反射,您可以访问类的方法,但在这种情况下,您需要知道要调用的方法的名称和输入参数类型。
继续该示例,更改步骤 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");

如果您在运行时没有提供所有这些信息,也许您需要重新考虑您的设计。

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.

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. 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.

柠檬心 2024-10-05 21:19:20

您应该使用 Java Reflection API:

Class c = Class.forName("package.name.Bean1");

那么您可以使用 c.newInstance() 来实例化您的类。该方法使用不需要参数的构造函数。

请参阅此处的详细信息:http://download.oracle.com/javase/tutorial/reflect/

You should use Java Reflection 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.

See details here: http://download.oracle.com/javase/tutorial/reflect/

难以启齿的温柔 2024-10-05 21:19:20

重复 Java 支持可变变量吗?

Java 不支持动态获取基于变量其名称的字符串(也称为变量)。可能有一种不同的方法来完成您想要做的事情,例如使用 Map 对象将名称映射到 bean。如果您编辑问题以更详细地解释您想要做什么,我们可能会得到一些更具体的答案。

(另一方面,如果问题是关于名为 Bean1 的类,那么 Kel 是对的。)

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.

(On the other hand, if the question was about a class called Bean1, then Kel's right.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文