package tw.com.javasorld;import java.lang.reflect.*;public class TestReflection03 {
public static void main(String[] args) { Class c;
Constructor constractor;
Object obj ;
try{ c= Class.forName("tw.com.javasorld.Person");
//调用有参数的构造方法
Class[] argTypes = new Class[]{String.class,int.class};
constractor = c.getConstructor(argTypes);
obj = constractor.newInstance("中国",5000);
System.out.println(obj);
//调用无参方法的构造函数
constractor = c.getConstructor();
obj = constractor.newInstance();
System.out.println(obj);
}catch(Exception e){
e.printStackTrace();
} }}package tw.com.javasorld;public class Person {
private int age;
private String name;
private int total;
Person(){
super();
total++;
}
Person(String name,int age){
super();
this.name = name;
this.age = age;
total++;
}
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;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String toString(){
return "Name :" +name +"\tAge :" + age;
}}第一个类运行不了,总报java.lang.NoSuchMethodException: tw.com.javasorld.Person.<init>(java.lang.String, int)错..可是Person中有相应构造方法.
不知道问题处在哪了?
public static void main(String[] args) { Class c;
Constructor constractor;
Object obj ;
try{ c= Class.forName("tw.com.javasorld.Person");
//调用有参数的构造方法
Class[] argTypes = new Class[]{String.class,int.class};
constractor = c.getConstructor(argTypes);
obj = constractor.newInstance("中国",5000);
System.out.println(obj);
//调用无参方法的构造函数
constractor = c.getConstructor();
obj = constractor.newInstance();
System.out.println(obj);
}catch(Exception e){
e.printStackTrace();
} }}package tw.com.javasorld;public class Person {
private int age;
private String name;
private int total;
Person(){
super();
total++;
}
Person(String name,int age){
super();
this.name = name;
this.age = age;
total++;
}
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;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String toString(){
return "Name :" +name +"\tAge :" + age;
}}第一个类运行不了,总报java.lang.NoSuchMethodException: tw.com.javasorld.Person.<init>(java.lang.String, int)错..可是Person中有相应构造方法.
不知道问题处在哪了?
试试
怎么会有这个方法?
,总报java.lang.NoSuchMethodException: tw.com.javasorld.Person. <init>(java.lang.String, int)错
换了还是报错
java.lang.NoSuchMethodException: tw.com.javasorld.Person.<init>(java.lang.String, java.lang.Integer)
constractor = c.getConstructor(argTypes);
obj = constractor.newInstance(argTypes);
参数传的不对
你用的什么ide呀 eclipse有提示呀
很容易就知道了
public Constructor<T> getConstructor(Class... parameterTypes)
throws NoSuchMethodException,
SecurityException返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法。parameterTypes 参数是 Class 对象的一个数组,这些 Class 对象按声明顺序标识构造方法的形式参数类型。
要反映的构造方法是此 Class 对象所表示的类的公共构造方法,其形式参数类型与 parameterTypes 所指定的参数类型相匹配。
参数:
parameterTypes - 参数数组
返回:
与指定的 parameterTypes 相匹配的公共构造方法的 Method 对象
抛出:
NoSuchMethodException - 如果找不到匹配的方法。
SecurityException - 如果存在安全管理器 s,并满足下列任一条件:
调用 s.checkMemberAccess(this, Member.PUBLIC) 拒绝访问构造方法
调用方的类加载器不同于也不是该类的类加载器的一个祖先,并且对 s.checkPackageAccess() 的调用拒绝访问该类的包
从以下版本开始:
JDK1.1