使用变量的值而不是其名称来声明新变量
假设我在java中有一个变量
String test=new String();
现在我想声明一个新变量,其名称等于变量“test”的值。
Suppose I've a variable in java
String test=new String();
Now i want to declare a new variable with its name equal to the value of the variable 'test'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Java 中无法做到这一点,请使用 java.util.Map 代替。
例如:
You can't do that in Java, use a java.util.Map instead.
For example:
这是不可能的,因为名称是在编译时固定的!
That is not possible, because the name is fix to compile time!
据我所知,Java不允许在反射中添加变量
但是,您可以使用
Map
来实现。现在,您通过以下方式寻找新对象:
amit
as far as I know, Java doesn't allow adding variables in reflection
however, you can use
Map<String,Object>
to achieve it.now, you seek your new objects by:
amit