java反射类报错

图片描述

package com.reflect;

public class Hello {

public static void main(String[] args) {

Class<?> demo=null;

try{

demo=Class.forName("com.reflect.Person");

}catch (Exception e) {

e.printStackTrace();

}

Person per=null;

try {

per=(Person)demo.newInstance();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

per.setName("Rollen");

per.setAge(20);

System.out.println(per);

}

}

package com.reflect;

class Person {

private String name;

private int age;

public 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;

}

@Override

public String toString() {

return super.toString();

}

}

回答:

Person类缺少无参构造方法;

以上是 java反射类报错 的全部内容, 来源链接: utcz.com/p/171245.html

回到顶部