这个反射方法程序有什么问题啊??
这是主函数
public static void main(String[] args)throws Exception {
Class ref=ReflectTest.class;
Constructor refConst=ref.getConstructor(int.class,String.class);
Object obj=refConst.newInstance(new ReflectTest(18,"wanggang"));
ReflectTest rf=(ReflectTest)obj;
int age=rf.getAge();
String name=rf.getName();
System.out.println(age + ":" + name);
}
这是ReflectTest...
package reflecttest;
public class ReflectTest {
int age;
String name;
public ReflectTest(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}为什么每次运行都是异常!!!
------解决方案--------------------
这句话明显写错了吧?
Object obj=refConst.newInstance(new ReflectTest(18,"wanggang"));
你都直接用new,还叫啥反射啊?应该是:
Object obj=refConst.newInstance(18,"wanggang");