javaBean的内省操作

来源:互联网 发布:淘宝网棉麻布鞋 编辑:程序博客网 时间:2024/05/16 05:42

一、内省对应的单词——》IntroSpector

二、主要是对javaBean进行操作;

三、什么是javaBean那?

javaBean是一种特殊的类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法符合某种规则,比如get()set();

四、有一个Person

public class Person {

private String name;

private int age;

Person(String name,int age)

{

this.name = name;

this.age = 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;

}

}

用内省的方式访问Personage或者name属性;

//属性描述符new PropertyDescriptor(propertyName,p.getClass());
  //用于得到某个类(p.getClass())的某个属性(propertyName)
  PropertyDescriptor pd2 =
   new PropertyDescriptor(propertyName,p.getClass());
  //有了属性就可以通过反射得到get()和set()方法;
  //得到get方法pd2.getWriteMethod()
  Method methodSetName = pd2.getWriteMethod();
  //调用这个方法;
  Object retVel = methodSetName.invoke(p,value);

 

原创粉丝点击