簡單理解Java的反射

反射(reflect):

  JAVA反射機制是在運行狀態中,對於任意一個實體類,都可以知道這個類的全部屬性和方法;對於任意一個對象,都可以調用它的任意方法和屬性;這種動態獲取信息以及動態調用對象方法的功能稱爲java語言的反射機制java

獲取類對象:

   經過查詢API得知:dom

  

  代碼以下:ide

  

 1 package com.baidu.reflect.test;
 2 /**
 3  * 測試獲取類對象
 4  * @author admin
 5  *
 6  */
 7 
 8 import static org.junit.Assert.*;
 9 
10 import org.junit.Test;
11 
12 import com.baidu.reflect.domain.People;
13 
14 public class Test01 {
15 
16     @Test
17     public void getClass01() throws Exception {
18         // 實例化對象獲取
19         People p = new People();
20         Class poeple = p.getClass();
21         System.out.println(poeple);
22         // com.baidu.reflect.domain.People
23     }
24 
25     @Test
26     public void getClass02() throws Exception {
27         // className.class獲取
28         Class people = People.class;
29         System.out.println(people);
30     }
31 
32     @Test
33     public void getClass03() throws Exception {
34         // 徹底限定名獲取
35         Class people = Class.forName("com.baidu.reflect.domain.People");
36         System.out.println(people);
37 
38     }
39 }

 

獲取構造方法:

  API以下:測試

 

代碼以下this

 

 1 package com.baidu.reflect.test;
 2 
 3 import java.lang.reflect.Constructor;
 4 
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 
 8 /**
 9  * 經過反射獲取構造方法
10  * 
11  * @author admin
12  *
13  */
14 public class Test02 {
15     // 初始化clazz對象
16     Class clazz = null;
17 
18     @Before
19     public void testObject() throws Exception {
20         // 獲取類對象
21         clazz = Class.forName("com.baidu.reflect.domain.People");
22     }
23 
24     @Test
25     public void testGetConstructor01() throws Exception {
26         // 經過類對象獲取無參構造
27         Constructor constructor = clazz.getConstructor();
28         // 經過構造獲取實例
29         Object people = constructor.newInstance();
30         System.out.println(people);// People [name=null, age=0, sex=null]
31 
32     }
33 
34     @Test
35     public void testGetConstructor02() throws Exception {
36         // 無參構造能夠直接獲取實例
37         Object people = clazz.newInstance();
38         System.out.println(people);
39     }
40 
41     @Test
42     public void testGetConstructor03() throws Exception {
43         // 經過對象獲有參構造
44         Constructor constructor = clazz.getConstructor(String.class, String.class);
45         // 獲取對象的實例並設置參數
46         Object people = constructor.newInstance("張三", "男");
47         System.out.println(people);// People [name=張三, age=0, sex=男]
48     }
49 
50     @Test
51     public void testGetConstructor04() throws Exception {
52         // 經過對象獲私有有參構造
53         Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class, String.class);
54         // 設置訪問權限
55         constructor.setAccessible(true);
56         // 獲取對象的實例並設置參數
57         Object people = constructor.newInstance("張三", 23, "男");
58         System.out.println(people);// People [name=張三, age=23, sex=男]
59     }
60 
61 }

 

 

獲取成員方法:

  查詢API得知:spa

  

  代碼以下code

  

 1 package com.baidu.reflect.test;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.Method;
 5 
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 import com.baidu.reflect.domain.People;
10 
11 /**
12  * 經過反射獲取方法 對象
13  * 
14  * @author admin
15  *
16  */
17 public class Test03 {
18     // 初始化clazz對象
19     Class clazz = null;
20     People people = null;
21 
22     @Before
23     public void testObject() throws Exception {
24         // 獲取類對象
25         clazz = Class.forName("com.baidu.reflect.domain.People");
26         // 向下轉型
27         people = (People) clazz.newInstance();
28     }
29 
30     @Test
31     public void testGetMethod01() throws Exception {
32         // 經過類對象獲取公共方法
33         Method setNameMethod = clazz.getMethod("setName", String.class);
34         // 設置方法參數
35         setNameMethod.invoke(people, "張三");
36         // 輸出people [name=張三, age=0, sex=null]
37         System.out.println(people + "\r" + "------------");
38         // 同上
39         Method getNameMethod = clazz.getMethod("getName");
40         String name = (String) getNameMethod.invoke(people);
41         System.out.println(name);// 張三
42     }
43 
44     @Test
45     public void testGetMethod02() throws Exception {
46         // 經過類對象獲取私有方法
47         Method getSayHiMethod = clazz.getDeclaredMethod("sayHi");
48         getSayHiMethod.setAccessible(true);
49         // 由於沒有返回值:Hello reflect
50         getSayHiMethod.invoke(people);
51 
52     }
53 
54 }

 

 

獲取成員變量:

  API:對象

  

  

  代碼:blog

  

 1 package com.baidu.reflect.test;
 2 
 3 import java.lang.reflect.Field;
 4 
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 
 8 import com.baidu.reflect.domain.People;
 9 
10 /**
11  * 經過反射獲取成員變量
12  * 
13  * @author admin
14  *
15  */
16 public class Test04 {
17     // 初始化clazz對象
18     Class clazz = null;
19     People people = null;
20 
21     @Before
22     public void testObject() throws Exception {
23         // 獲取類對象
24         clazz = Class.forName("com.baidu.reflect.domain.People");
25         // 向下轉型
26         people = (People) clazz.newInstance();
27     }
28 
29     @Test
30     public void testGetField01() throws Exception {
31         // 經過類對象獲取private field;
32         Field nameField = clazz.getDeclaredField("name");
33         nameField.setAccessible(true);
34         nameField.set(people, "張三");
35         System.out.println(people);// People [name=張三, age=0, sex=null]
36 
37     }
38 
39     @Test
40     public void testGetField02() throws Exception {
41         // 經過類對象獲取public field
42         Field sexField = clazz.getField("sex");
43         sexField.set(people, "未知");
44         System.out.println(people);// People [name=null, age=0, sex=未知]
45 
46     }
47 
48 }

 

 

實體類:

  

 1 package com.baidu.reflect.domain;
 2 
 3 /**
 4  * 實體
 5  * 
 6  * @author admin
 7  *
 8  */
 9 public class People {
10 
11     private String name;
12     private int age;
13     public String sex;
14 
15     public People() {
16     }
17 
18     private People(String name, int age, String sex) {
19         this.name = name;
20         this.age = age;
21         this.sex = sex;
22     }
23 
24     public People(String name, String sex) {
25         this.name = name;
26         this.age = age;
27         this.sex = sex;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public int getAge() {
39         return age;
40     }
41 
42     public void setAge(int age) {
43         this.age = age;
44     }
45 
46     public String getSex() {
47         return sex;
48     }
49 
50     public void setSex(String sex) {
51         this.sex = sex;
52     }
53 
54     private void sayHi() {
55         System.out.println("Hello reflect");
56     }
57 
58     @Override
59     public String toString() {
60         return "People [name=" + name + ", age=" + age + ", sex=" + sex + "]";
61     }
62 }

 

 

 但願能給你們一個參考,也但願你們多多支持我。get

 

查閱文獻:    https://baike.baidu.com/item/JAVA反射機制/6015990?

相關文章
相關標籤/搜索