黑马程序员java笔记之三JavaBean

IntroSpector---内省:用于对JavaBean操作。

JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。

把一个类当做JavaBean来看,JavaBean的属性是根据getAge()和setAge(int age)方法名称推断出来的,不是根据内部成员变量推断的!eg:

  1. <span style="font-size:18px;">class Person{  
  2.      private int x;  
  3.      public int getAge(){  
  4.      return x;  
  5.       }  
  6. public void setAge(int age){int age}{  
  7.   this.x=age;  
  8.      }  
  9. }</span>  



把Person当做一个JavaBean对象来看,有一个age属性。JavaBean属性特点,去掉set、和ge前缀,剩下来的属性就是JavaBean的属性名。属性命名规则:

eg: Age------>a如第二个字母小写,则把首字母变成小写--->age

方法对应的JavaBean属性:

getTime----->time    setTime---->time        getCUP----->CPU

 JavaBean作用:

1.       在java EE开发中,经常使用JavaBean,环境要求按JavaBean操作。

2.       JDK提供了对JavaBean进行操作的一些API,这套API就称为内省。用内省的API操作JavaBean比普通类的方式更方便。

补充:

可以将模块传递信息封装到JavaBean中,JavaBean的实例对象通常称之为值对象(Value Object,简称VO)。简单内省操作eg:

                  

  1. class ReflectionPoint{    
  2.    private int x;    
  3.    pirvate int y;   
  4.    public ReflectionPoint(int x,int y){    
  5.      this.x = x;    
  6.      this.y = y;    
  7.     }    
  8.   public void setX(int x){    
  9.         this.x = x;    
  10.     }    
  11.     public int getX(){    
  12.         return x;    
  13.     }    
  14.     public void setY(int y){    
  15.         this.y = y;    
  16.     }    
  17.     public int getY(){    
  18.         return y;    
  19.  }  



方法一:

  1. <span style="font-size:18px;">publicclass IntroSpectorTestone {  
  2.    publicstaticvoid main(String[] args)throws Exception {  
  3.      ReflectPoit s=new ReflectPoit(35);  
  4.      String x="x";  
  5.     ObjectretValue =getProperty(s, x);       
  6.     System.out.println(retValue);  
  7.     Object obj=7;  
  8.     setProperty(s, x, obj);  
  9.     System.out.println(s.getX())  
  10.    }</span>  

//思路:PropertyDescriptor---->getReadMethod------>invoke

   

  1. privatestaticObjectgetProperty(Objects, String x)  
  2.   
  3.            throws IntrospectionException, IllegalAccessException,  
  4.   
  5.            InvocationTargetException {  
  6.   
  7.        PropertyDescriptor  pd=new PropertyDescriptor(x, ReflectPoit.class);  
  8.   
  9.          //PropertyDescriptor属性描述符,得到目标类的属性描述符  
  10.        Method   methodGetX=pd.getReadMethod();;//得到可读的方法  
  11.        ObjectretValue=methodGetX.invoke(s); //调用相应对对象可写的方法,返回属性值  
  12.        return retValue;  
  13.     }  
  14.    private static Object getProperty(Object s, String x)  
  15.       throws IntrospectionException, IllegalAccessException,      InvocationTargetException {  
  16.          PropertyDescriptor  pd=newPropertyDescriptor(x, ReflectPoit.class);  
  17.          Method  methodGetX=pd.getReadMethod();  
  18.          Object retValue=methodGetX.invoke(s);  
  19.          return retValue;  
  20. }  
  21. }  



 

个人总结:JavaBean中通过内省的方法到相应属性的getset方法。

技巧:代码抽取成方法:Refactor-----Extract Method

只要调用了这个方法,并给这个方法传递了一个对象、属性名和设置值,它就能完成属性修改的功能。

得到BeanInfo最好采用“obj.getClass()方式”,而不要采用“类名.class方式”这样程序更通用。

方法二:

采用遍历BeanInfo的所有属性方法来查找和设置某个RefectPoint对象的x属性。在程序中把一个类当做JavaBean来看,就是调用IntroSpector.getBeanInfo方法,得到的BeanInfo对象封装了把这个类当做JavaBean看的结果信息。核心代码:

//思路: BeanInfo---->PropertyDesciptor----->getReadMethod()---->invoke

  1. <span style="font-size:18px;">    BeanInfo beanInfo=Introspector.getBeanInfo(ReflectPoit.class);  
  2.      PropertyDescriptor[]pd=beanInfo.getPropertyDescriptors();  
  3.       Object retValue=null;  
  4.         for(PropertyDescriptorpt:pd){//PropertyDescriptor属性描述符  
  5.         if(pt.getName().equals(x)){  
  6.         MethodmethodGet=pt.getReadMethod();//得到可读的方法  
  7.        retValue=methodGet.invoke(s);//返回相应对象的属性值  
  8.        }  
  9.       }</span>  



Java 7新特性 Mapmap={name:”xxxx”,age:121};map的定义方法

BeanUtils是以字符串进行操作,PropertyUtils是以本身的类型进行操作

原文地址:https://www.cnblogs.com/lixiaolun/p/2832756.html