内省,BeanInfo

来源:互联网 发布:landesk软件下载 编辑:程序博客网 时间:2024/05/22 12:48
public class Test_relect2 {public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {BeanInfo info = Introspector.getBeanInfo(Man.class,Object.class);PropertyDescriptor[] b = info.getPropertyDescriptors();for (PropertyDescriptor pd : b) {System.out.println(pd.getName() +"=="+pd.getPropertyType());}Man m = new Man();//Method method = b[0].getWriteMethod();//method.invoke(m, 12);////method = b[0].getReadMethod();//System.out.println(method.invoke(m, null));PropertyDescriptor pd = new PropertyDescriptor("name", Man.class);Method method = pd.getWriteMethod();method.invoke(m, "coci");method = pd.getReadMethod();System.out.println(method.invoke(m,null));;System.out.println();}}
class Man{private String name ;private int age;private float money;public String getName() {return name;}public void invoke(Man m, String string) {// TODO Auto-generated method stub}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public float getMoney() {return money;}public void setMoney(float money) {this.money = money;}}


0 0
原创粉丝点击