Java:强制字符串名称被接受为类?

发布于 2024-12-10 20:20:19 字数 612 浏览 0 评论 0原文

我想打印出这样的语句:

Object SomeObject = new Object("object's name");
System.out.println(SomeObject.name);

除了不在打印语句中指定对象的名称之外,还指定一个先前已声明为现有对象的名称的字符串。

示例:

Object SomeObject = new Object("object's name");
String object_string = "SomeObject";
System.out.println("object_string.name");

我知道第一个示例会打印出“对象的名称”。我希望第二个做同样的事情,但使用字符串。请注意,字符串的值与对象名称相同。显然第二个例子不起作用,因为 object_string 是一个字符串,而不是一个对象。

所以我的问题是,我可以强制打印语句接受“object_string”作为对象的名称吗?

System.out.println("(Object)object_string.name");

也许是这样的?

谢谢

I want to print out a statement like so:

Object SomeObject = new Object("object's name");
System.out.println(SomeObject.name);

except rather than specifying the name of the Object in the print statement, specify a String which would have earlier been declared as the name of an existing object.

Example:

Object SomeObject = new Object("object's name");
String object_string = "SomeObject";
System.out.println("object_string.name");

I know that the first example would print out "object's name". I want the second one to do the same, but using the string. Note that the value of the string is the same as the object name. Obviously the second example won't work, because object_string is a string, not an object.

So my question is, can I force the print statement to accept "object_string" as the name of an Object?

System.out.println("(Object)object_string.name");

Something like that perhaps?

Thanks

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

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

发布评论

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

评论(2

小…红帽 2024-12-17 20:20:19

您正在寻找的称为“字符串插值”(至少在 Perl 中是这么称呼的,这是我最熟悉的示例),而 Java 没有它,抱歉。然而,有一些基于 Java 构建的工具可以在更专业的用例中提供类似的功能。例如,您可能对 JSP EL 感兴趣,< a href="http://commons.apache.org/ognl/" rel="nofollow">OGNL,或 Spring EL

但请注意,所有这些都需要设置一些脚手架,特别是,您在这些动态上下文中引用的对象必须首先放置到某种地图或其他容器中。无法直接从局部变量引用它们。

另一方面,Groovy 等其他一些语言也内置了此功能。

What you're looking for is called String Interpolation (at least that's what it's called in Perl, which is the example I'm most familiar with) and Java doesn't have it, sorry. However, there are some tools built on top of Java that offer similar functionality in more specialized use cases. You might, for example, be interested in JSP EL, OGNL, or Spring EL.

Note, however, that all of these require some scaffolding to set up, and in particular, the objects you reference in these dynamic contexts must be first placed into some kind of map or other holder. There's no way to reference them directly from a local variable.

On the other hand, some other languages like Groovy offer this feature built-in.

水中月 2024-12-17 20:20:19

您无法从 String 转到命名变量名称,当编译为字节代码时,object_string 变量不再存在,并且编译器无法知道“object_string”引用变量 object_string。当然,除非您有一个字符串到您控制的变量的映射。

您最好使用反射来查找属性“名称”并打印它。

public void printProperty(Class<?> cl, String propertyName) {
   //Use reflection here to look up the propertyName on class cl and print
}

printProperty(Object.class, 'name');

You cannot go from String to a named variable name, object_string variable no longer exists when compiled to byte code and there is no way for the compiler to know that "object_string" references the variable object_string. That is of course unless you have a map of string to variables you control.

You're better off using reflection to look for a property 'name' and printing that.

public void printProperty(Class<?> cl, String propertyName) {
   //Use reflection here to look up the propertyName on class cl and print
}

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