创建对象我想这样做与提供的名称
class DemoClass {
int money =1000;
void display() {
System.out.println("money=" + money);
}
}
public class createobject {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String objectname = "Harry";
DemoClass objectname=new DemoClass();//creating object with a supplied name
System.out.println(Harry.money);//will print 1000
Harry.display();// will print the same
}
}
它给错误。有没有其他解决方案的方法。
'这是给错误。“ - 你会得到什么错误?你究竟想实现什么?通过动态创建的变量名引用对象在Java中不起作用。改用'Map'。 –
尽管这不会工作,你认为应该用动态名称创建变量的好处是什么? – SomeJavaGuy
[Java变量值作为新变量名]的可能重复(http://stackoverflow.com/questions/9152439/java-variable-value-as-new-variable-name) –