目的:通过反射获得Person类得构造函数,进而获得一个Person的实例
不过下面代码报出了错误 ;请指点迷津
package com.itheima;
import java.lang.reflect.*;
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class PersonDemo {
public static void main(String[] args) throws Exception {
Constructor constructor = Person.class.getConstructor(String.class,int.class);
Person person = (Person)constructor.newInstance(new Person("zhangsan",25));
System.out.println(person.getName()+":"+person.getAge());
}
} |
|