//main方法
public static void main(String[] args) {
Person obj = new Person();
//获取类对象,类型
Class clazz = obj.getClass();
//获取类对象的 所属性字段 数组
Field[] fields = clazz.getDeclaredFields();
int i=0;
for (Field field : fields ) {
try {
//获取单个字段属性的 类型
Class<?> varName1 = field.getType();
PropertyDescriptor propertyDescriptor = null;
//遍历所方法
propertyDescriptor = new PropertyDescriptor(field.getName(), obj.getClass());
//得到set方法
Method setMethod = propertyDescriptor.getWriteMethod();
if(varName1 == java.util.Date.class){
//执行set方法
setMethod.invoke(obj, new Date());
}else if(varName1 == int.class){
//执行set方法
setMethod.invoke(obj,1);
}else{
Object s=i+1;
//执行set方法
setMethod.invoke(obj, s.toString() );
}
// 获得get方法
Method getMethod1 = propertyDescriptor.getReadMethod();
//执行(get方法,获取get方法返回的结果值
Object val = getMethod1.invoke(obj);
i++;
System.out.println(val);
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}//end for
}
//实体类
public class Person {
private int age;
private String name;
private Date birthday;
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;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}