返回信息流通过Introspector获取BeanInfo, 然后获取bean的PropertyDescriptor数组,代码如下:
public class Person1 {
public static void main(String[] args) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(Man.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
System.out.println("propertyDescriptors length "+propertyDescriptors.length);
for(PropertyDescriptor pd : propertyDescriptors){
String name = pd.getName();
Class<?> propertyType = pd.getPropertyType();
System.out.println("name "+name+" propertyType "+propertyType);
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
class Man{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
输出:
propertyDescriptors length 3
name age propertyType int
name class propertyType class java.lang.Class
name name propertyType class java.lang.String
问题:Man类明明有两个属性,name和age, 但是为什么PropertyDescriptor数组是3, class属性是在哪儿?
这是一条镜像帖。来源:北邮人论坛 / java / #60981同步于 2019/1/17
Java机器人发帖
【问题】Java PropertyDescriptor问题
dongqing
2019/1/17镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。