反射-私有变量/私有方法-Java

Posted fangke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射-私有变量/私有方法-Java相关的知识,希望对你有一定的参考价值。

 1 import java.lang.reflect.Field;
 2 import java.lang.reflect.InvocationTargetException;
 3 import java.lang.reflect.Method;
 4 
 5 /**
 6  * Created with IDEA
 7  * author:foreign
 8  * Date:2019/9/30
 9  * Time:10:40
10  */
11 public class ReflectionFk {
12     public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
13         PersonFk personFk = new PersonFk();
14         personFk.setName("foreign");
15 
16         Class clazz = PersonFk.class;
17         //1 获取全部private变量
18         Field[] declaredFields = clazz.getDeclaredFields();
19         for (Field field : declaredFields) {
20             field.setAccessible(true);
21             System.out.println("获取private字段:" + field);
22         }
23         //1.1 获取指定的private变量
24         Field name = clazz.getDeclaredField("name");
25         name.setAccessible(true);
26         String str = (String) name.get(personFk);
27         System.out.println("name字段值:" + str);
28 
29         //2 获取全部private方法
30         Method[] declaredMethods = clazz.getDeclaredMethods();
31         for (Method method : declaredMethods) {
32             method.setAccessible(true);
33             System.out.println("获取private方法: " + method);
34         }
35         //2.1 获取指定private方法
36         PersonFk personFk1 = new PersonFk("123");
37         Method getDepartmentId = clazz.getDeclaredMethod("getDepartmentId", null);
38         getDepartmentId.setAccessible(true);
39         String returnStr = (String) getDepartmentId.invoke(personFk1, null);
40         System.out.println("获取指定的private方法:" + returnStr);
41     }
42 }

实体类

 1 /**
 2  * Created with IDEA
 3  * author:foreign
 4  * Date:2019/9/30
 5  * Time:10:39
 6  */
 7 public class PersonFk {
 8     private String name;
 9     private Integer age;
10     private Boolean gender;
11     public String desc;
12     private String departmentId;
13 
14     public PersonFk(String name, Integer age, Boolean gender, String desc, String departmentId) {
15         this.name = name;
16         this.age = age;
17         this.gender = gender;
18         this.desc = desc;
19         this.departmentId = departmentId;
20     }
21 
22     public PersonFk(String departmentId) {
23         this.departmentId = departmentId;
24     }
25 
26     public PersonFk() {
27     }
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public Integer getAge() {
38         return age;
39     }
40 
41     public void setAge(Integer age) {
42         this.age = age;
43     }
44 
45     public Boolean getGender() {
46         return gender;
47     }
48 
49     public void setGender(Boolean gender) {
50         this.gender = gender;
51     }
52 
53     public String getDesc() {
54         return desc;
55     }
56 
57     public void setDesc(String desc) {
58         this.desc = desc;
59     }
60 
61     private String getDepartmentId() {
62         return departmentId;
63     }
64 
65     public void setDepartmentId(String departmentId) {
66         this.departmentId = departmentId;
67     }
68 }

 

以上是关于反射-私有变量/私有方法-Java的主要内容,如果未能解决你的问题,请参考以下文章

.net 反射访问私有变量和私有方法

反射机制访问私有方法代码异常"java.lang.ClassCastException"

如何限制开发人员使用反射访问Java中的私有方法和构造函数?

既然Java反射可以访问和修改私有成员变量,那封装成private还有意义么?

java反射基础知识反射应用实践

Java通过反射可以设置私有变量的好处有哪