黑马程序员技术交流社区
标题:
大家看看这代码错在哪
[打印本页]
作者:
马胜平
时间:
2012-3-2 18:53
标题:
大家看看这代码错在哪
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Person{
private String name;
public Integer age;
public Person(){
}
public Person(String name,Integer age){
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(Integer age) {
this.age = age;
}
public int getAge() {
return age;
}
}
public class JavaBeanTest{
public static void main(String[]args){
Class clazz=Person.class;
try {
Person p=(Person) clazz.newInstance();
p.setName("jay_ma");
String propertyName="age";
PropertyDescriptor pd=new PropertyDescriptor(propertyName,clazz);
Method method=pd.getWriteMethod();
method.invoke(p, new Integer(20));
System.out.println("姓名:"+p.getName()+"年龄:"+p.getAge());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
复制代码
明明有这个方法为啥老提示找不到异常java.beans.IntrospectionException: Method not found: setAge
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:93)
at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:53)
at com.itheima.JavaBeanTest.main(JavaBeanTest.java:47)
作者:
杨盼
时间:
2012-3-2 19:14
把Integer换成int就行了
作者:
杨盼
时间:
2012-3-2 19:20
你那个getAge返回的类型和设置的类型不一致
作者:
马胜平
时间:
2012-3-2 19:32
谢谢大家
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2