花开花落总相似 发表于 2013-5-7 14:35
看不出来 代码不全没法运行
package java.oop;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
public class Test {
/**
* @param args
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
IntrospectorDemo demo = new IntrospectorDemo();
demo.setName("张三");
BeanInfo bi = Introspector.getBeanInfo(demo.getClass(),Object.class);
PropertyDescriptor[] props = bi.getPropertyDescriptors();
for(int i=0;i<props.length;i++){
System.out.println(props.getName()+"="+props.getReadMethod().invoke(demo,null));
}
}
IntrospectorDemo demo = new IntrospectorDemo(); //是一个类 里面的属性有
public class IntrospectorDemo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
} |