[Java] 纯文本查看 复制代码
package com.itheima.bean;
public class Person {
private String name;
private int 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 + "]";
}
}
[Java] 纯文本查看 复制代码
package com.itheima.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import com.itheima.bean.Person;
public class IntrospectorDemo {
public static void main(String[] args) throws IntrospectionException {
//1.实例化一个Person
Person beanObj = new Person();
//2.依据Person产生一个相关的BeanInfo类
BeanInfo infoObj = Introspector.getBeanInfo(beanObj.getClass(),
beanObj.getClass().getSuperclass());
String str="内省成员属性:\n";
//3.获取该Bean中的所有属性的信息,以PropertyDescriptor数组的形式返回
PropertyDescriptor[] propertyArray = infoObj.getPropertyDescriptors();
for(int i=0;i<propertyArray.length;i++){
//获取属性名
String propertyName = propertyArray.getName();
//获取属性类型
Class propertyType = propertyArray.getPropertyType();
str += propertyName+"("+propertyType.getName()+")\n";
}
System.out.println(str);
}
}