class Test{
public static void main(String[] args)throws Exception{
Constructor personconstructor = Person.class.getConstructor(String.class,int.class);
Person liuchao = (Person)personconstructor.newInstance("liuchao",25);
}
}
class Person{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
}
运行时报错:Exception in thread "main" java.lang.NoSuchMethodException: Person.<init>(java.lang.String, int)
at java.lang.Class.getConstructor0(Class.java:2721)
at java.lang.Class.getConstructor(Class.java:1674)
at Test02.main(Test02.java:27) |
|