本帖最后由 Friendy89 于 2013-8-31 01:04 编辑
各位大侠帮忙看下,这段代码有什么问题,为什么通过反射创建的Person对象,无法获取name和age- import java.lang.reflect.Constructor;
- public class ReflectTest {
- /**
- * @param args
- * @throws Exception
- * @throws SecurityException
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- Constructor constructor = Person.class.getConstructor(String.class,int.class);
- Person person = (Person) constructor.newInstance("wbl",24);
- System.out.println(person.getName()+":::"+person.getAge());
- }
- }
- class Person {
- private String name ;
- private int age ;
- Person(String name, int age) {
- super();
- 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;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + "]";
- }
- }
复制代码
|