PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors()方法用于获取的类的属性
获取的属性不是类的成员变量。
例如:
package junit.test;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class Bean {
public String name;
public void setNameTest(String name) {
this.name = name;
}
public static void main(String[] args) {
try {
PropertyDescriptor[] pds = Introspector.getBeanInfo(Bean.class).getPropertyDescriptors();
for(PropertyDescriptor pd : pds){
System.out.println(pd.getName());
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
输出的结果为
class
nameTest所以类的成员变量不是类的属性
本文介绍如何使用Java反射机制Introspector获取Bean类的属性,并通过示例代码展示获取到的属性并非直接对应的成员变量,而是由getter和setter方法定义的属性。
3221

被折叠的 条评论
为什么被折叠?



