public class Test {
public static void main(String[] args) throws Exception {
Car c= (Car) Class.forName("Car").newInstance();
System.out.println(c.getName());
}
}
class Car {
String name = "Default Car";
String getName(){return this.name;}
}
清楚的代码。
但是,如果我添加的构造函数使用参数,有的像这样:
public Car(String name)
{this.name = name;}
我看到:java.lang.InstantiationException
所以,不,我不知道如何使用参数传递的构造。
请帮忙。
对于本机类型,请使用(例如)Integer.TYPE而不是Integer.class – mcoolive 2017-12-15 17:25:56
@mcoolive或int.class – 2017-12-16 03:28:12