public class D12反射创建对象两种方式 {
public static void main(String[] args) throws Exception {
Class<Pn> clazz = Pn.class;
Pn p = (Pn) clazz.newInstance();
Constructor con = clazz.getConstructor(String.class);
Pn p2 = (Pn) con.newInstance("在");
p.run();
p2.run();
}
}
class Pn {
String str;
public Pn() {
super();
}
public Pn(String str) {
this.str = str;
}
public void run() {
System.out.println("1111111111111");
}
} |
|