本帖最后由 strawberry2013 于 2013-5-15 17:08 编辑
import java.lang.reflect.*;
class Demo1
{
public static void main(String[] args) throws Exception
{
Person p = new Person("s",20);
Constructor<Person> con = Person.class.getConstructor(String.class, int.class); //error
//Exception in thread "main" java.lang.NoSuchMethodException: Person.<init>(java.lang.String, int)
Person p1 = con.newInstance("zhangsan", 22);
p1.show();
}
}
class Person
{
private int age;
private String name;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
public void show()
{
System.out.println(name+":"+age);
}
}
//在获取构造方法时,不知道哪里出现了错误,实在不解,烦请个为同学帮帮忙~~
|
|