關於反射的基本應用--將一個對象中的所有屬性值賦值給另一個對象


 

在開發中,存在DO,BO,VO之類的模型,在從DO到BO或從BO到VO的過程中,我們通常要進行取值賦值的一系列操作,非常繁雜,該方法主要將這件事情進行抽取,將繁雜事情簡單化

1.定義BO

 1 /**
 2  * Created by Yanwu on 2018/3/1.
 3  */
 4 public class TestBo {
 5     private Long id;
 6     private String name;
 7     private int age;
 8     private Boolean sex;
 9     private Double height;
10 
11     public Double getHeight() {
12         return height;
13     }
14 
15     public void setHeight(Double height) {
16         this.height = height;
17     }
18 
19     public Long getId() {
20         return id;
21     }
22 
23     public void setId(Long id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public int getAge() {
36         return age;
37     }
38 
39     public void setAge(int age) {
40         this.age = age;
41     }
42 
43     public Boolean getSex() {
44         return sex;
45     }
46 
47     public void setSex(Boolean sex) {
48         this.sex = sex;
49     }
50 }

2.定義VO

 1 /**
 2  * Created by Yanwu on 2018/3/1.
 3  */
 4 public class TestVo {
 5     private Long id;
 6     private String name;
 7     private int age;
 8     private Boolean sex;
 9     private Double weight;
10 
11     public Double getWeight() {
12         return weight;
13     }
14 
15     public void setWeight(Double weight) {
16         this.weight = weight;
17     }
18 
19     public Long getId() {
20         return id;
21     }
22 
23     public void setId(Long id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public int getAge() {
36         return age;
37     }
38 
39     public void setAge(int age) {
40         this.age = age;
41     }
42 
43     public Boolean getSex() {
44         return sex;
45     }
46 
47     public void setSex(Boolean sex) {
48         this.sex = sex;
49     }
50 
51     @Override
52     public String toString() {
53         return "TestVo{" +
54                 "id=" + id +
55                 ", name='" + name + '\'' +
56                 ", age=" + age +
57                 ", sex=" + sex +
58                 ", weight=" + weight +
59                 '}';
60     }
61 }

3.轉換的工具函數

 1 public <T> T gotObjectByObject(Object object, Class<T> clazz) throws Exception {
 2     T t = null;
 3     if (clazz != null && object != null) {
 4         t = clazz.newInstance();
 5         Field[] fields = clazz.getDeclaredFields();
 6         for (Field field : fields) {
 7             field.setAccessible(true);
 8             String key = field.getName();
 9             try {
10                 Field field1 = object.getClass().getDeclaredField(key);
11                 field1.setAccessible(true);
12                 Object val = field1.get(object);
13                 field.set(t, val);
14             } catch (Exception e) {
15                 System.out.println(object.getClass().getName() + "沒有該屬性: " + key);
16             }
17         }
18     }
19     return t;
20 }

4.測試

 1 @Test
 2 public void testObject() throws Exception {
 3     TestBo bo = new TestBo();
 4     bo.setId(1L);
 5     bo.setName("測試");
 6     bo.setAge(28);
 7     bo.setSex(true);
 8     bo.setHeight(12.32);
 9     TestVo vo = gotObjectByObject(bo, TestVo.class);
10     System.out.println(vo.toString());
11 }

5.輸出結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM