黑馬程序員_BeanUtils包的使用,主要是BeanUtils和PropertyUtils的區別

  ----------------------    android培訓    java培訓   期待與您交流!    ---------------------- 


用BeanUtils工具包時,先要把兩個Jar包進行Building Path,就是引入兩個jar包,

commons-beanutils.jar

commons-logging-1.1.jar

  1. package com.base_super;  
  2.   
  3. import java.util.Date;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.commons.beanutils.BeanUtils;  
  7. import org.apache.commons.beanutils.PropertyUtils;  
  8.   
  9. /** 
  10.  * Apache 提供的BeanUtils工具包 
  11.  *  
  12.  * @author zjw 
  13.  * 
  14.  */  
  15. public class BeanUtils_class {  
  16.     public static void main(String[] args)throws Exception {  
  17.         methiod();  
  18.     }  
  19.     public static void methiod() throws Exception {  
  20.         JavaBean_BeanUtils_class bean=new JavaBean_BeanUtils_class(33,44);  
  21.         //如果用BeanUtils調用的JavaBean 一定要用public修飾,要不然報錯  
  22.         System.out.println(BeanUtils.getProperty(bean,"b"));//其他數據類型都自動轉換爲字符串類型,進行輸入輸出  
  23.         System.out.println(BeanUtils.getProperty(new JavaBean_BeanUtils_class(333,888),"a"));  
  24.         BeanUtils.setProperty(bean,"str","aaaaaaaaaaaaaaaaaaaaaa");//通過BeanUtils設置JavaBean的值  
  25.         System.out.println(bean.getStr());  
  26.         //BeanUtils支持屬性鏈  
  27.         Date d=new Date();  
  28. //      d.setHours(hours);  
  29. //      d.setMinutes(minutes);  
  30. //      d.setMonth(month);  
  31. //      d.setSeconds(seconds);  
  32. //      d.setYear(year);  
  33. //      d.setTime(time);  
  34. //      d.setDate(date);  
  35. //      d.getDay();//這個getDay不行,所以一般用屬性鏈時,最好用set方法的屬性(自我總結)  
  36.         //Date中封裝了這些個屬性,都可以用做屬性鏈,但每個屬性都有自己的長度等限制,用時小心點  
  37.         BeanUtils.setProperty(bean,"birthday.time","333333");  
  38.         System.out.println(BeanUtils.getProperty(bean,"birthday.time"));  
  39.           
  40.           
  41.         /* 
  42.          * Map和JavaBean都是屬性,值的組合方式,很相似,可以同過BeanUtils工具相互轉換 
  43.          *  
  44.          */  
  45.           
  46.         //這是JDK1.7的新特性,Map的新的定義方式  
  47. //      Map map={name:"wjw",age:23};  
  48. //      System.out.println(BeanUtils.setProperty(map,"name","wjw_java"));//BeanUtils也可以對Map進行操作  
  49.           
  50.         /* 
  51.          * BeanUtils以字符串類型進行操作 
  52.          * PropertyUtils以數據本身的類型進行操作 
  53.          */  
  54.         PropertyUtils.setProperty(bean,"str","3331111");//str本身是字符串類型  
  55.         System.out.println(PropertyUtils.getProperty(bean,"str"));  
  56.           
  57.         PropertyUtils.setProperty(bean,"a",99999999);//a本身是整形數據類型  
  58.         System.out.println(PropertyUtils.getProperty(bean,"a"));  
  59.       
  60.     }  
  61. }  
  62.   
  63. 對應的JavaBean  
  64. package com.base_super;  
  65.   
  66. import java.util.Date;  
  67.   
  68. public class JavaBean_BeanUtils_class{  
  69.     private int a;  
  70.     private int b;  
  71.     private String str="dddddddd";  
  72.     private Date birthday =new Date();//實例化對象,用BeanUtils就可以使用Date中的屬性了  
  73.       
  74.       
  75.     public JavaBean_BeanUtils_class(int a,int b){  
  76.         this.a=a;  
  77.         this.b=b;  
  78.     }  
  79.       
  80.     public int getA() {  
  81.         return a;  
  82.     }  
  83.     public void setA(int a) {  
  84.         this.a = a;  
  85.     }  
  86.     public int getB() {  
  87.         return b;  
  88.     }  
  89.     public void setB(int b) {  
  90.         this.b = b;  
  91.     }  
  92.   
  93.     public String getStr() {  
  94.         return str;  
  95.     }  
  96.   
  97.     public void setStr(String str) {  
  98.         this.str = str;  
  99.     }  
  100.   
  101.     public Date getBirthday() {  
  102.         return birthday;  
  103.     }  
  104.   
  105.     public void setBirthday(Date birthday) {  
  106.         this.birthday = birthday;  
  107.     }  
  108.   
  109.        
  110.       
  111. }  
  ----------------------    android培訓    java培訓   期待與您交流!    ---------------------- 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章