黑马程序员_BeanUtils包的使用,主要是BeanUtils和PropertyUtils的区别

  ----------------------    android培训    java培训   期待与您交流!    ---------------------- 


用BeanUtils工具包时,先要把两个Jar包进行Building Path,就是引入两个jar包,

commons-beanutils.jar

commons-logging-1.1.jar

[java]  view plain copy print ?
  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培训    期待与您交流!    ---------------------- 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值