用beanutils操纵javabean(不含自定义字符类型转换功能)

 1 package beanutils;
 2 
 3 import javax.xml.crypto.Data;
 4 
 5 public class person {   //javabean
 6     private String name;//字段
 7     private int age;//字段
 8     private String password;//字段
 9 
10     public String getName()
11     {
12         return name;
13         
14     }
15     public int getAge()
16     {
17         return age;
18         
19     }
20     public String getPassword()
21     {
22         return password;
23         
24     }
25     public void setName(String name)
26     {
27         this.name=name;
28     }
29     public void setAge(int age)
30     {
31         this.age=age;
32     }
33     public void setPassword(String password)
34     {
35         this.password=password;
36     }
37 
38 }
 1 package beanutils;
 2 
 3 import java.lang.reflect.InvocationTargetException;
 4 
 5 import org.apache.commons.beanutils.BeanUtils;
 6 import org.apache.commons.beanutils.ConvertUtils;
 7 import org.junit.Test;
 8 
 9 //使用beanutils操纵bean的属性
10 public class demo {
11   @Test
12   public void test1() throws Exception, Exception
13   {
14       person p=new person();
15       BeanUtils.setProperty(p,"name","hemaoyun");//实现这个功能需要导入beanutils包(自己导入第三方jar)
16       System.out.println(p.getName());
17   }
18   @Test
19   public void test2() throws Exception, Exception
20   {
21       String name="aaaa";
22       String password="1234";
23       String age="12";
24       person p=new person();
25       BeanUtils.setProperty(p,"name",name);
26       BeanUtils.setProperty(p,"password",password);
27       BeanUtils.setProperty(p,"age",age);//只支持8种数据类型转换(beanutils自动把String类型转换成int类型)
28       System.out.println(p.getAge());
29       System.out.println(p.getName());
30       System.out.println(p.getPassword());
31   }
32  //除了8中基本数据类型的其他类型
33   @Test
34   public void test3() throws Exception, Exception
35   {
36       String name="aaaa";
37       String password="1234";
38       String age="12";
39   
40               person p=new person();
41       
42       BeanUtils.setProperty(p,"name",name);
43       BeanUtils.setProperty(p,"password",password);
44       BeanUtils.setProperty(p,"age",age);//只支持8种数据类型转换(beanutils自动把String类型转换成int类型)
45     
46       
47       System.out.println(p.getAge());
48       System.out.println(p.getName());
49       System.out.println(p.getPassword());
50      
51   }
52 }

 

posted on 2015-01-26 18:55  曾经的码农  阅读(141)  评论(0编辑  收藏  举报