我是靠谱客的博主 小巧冰淇淋,这篇文章主要介绍反射之利用反射获取类方法,字段构造方法,并进行操作注意看代码里面的注释,现在分享给大家,希望可以做个参考。

反射可以用来做很多事情,比方说获取一个类的构造方法,定义的方法,字段,还可以动态改变属性的值,动态调用类方法等等...

注意看代码里面的注释

现在我们定义一个 Student 类,类有 三个属性,setget 方法,无参和有参构造方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** * 学生类 * @author snow * */ public class Student { @StudentName(value="zhangzq") public String name; @StudentSchool(value="清华大学") public String school; @StudentAge(value=17) public int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", school=" + school + ", age=" + age + "]"; } public Student() { super(); } public Student(String name, String school, int age) { super(); this.name = name; this.school = school; this.age = age; } }

1,获取所有的构造方法,并使用class对象进行实例化对象

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
Class<?> clazz = Class.forName("com.zzq.reflect.anotation.Student"); // 获取的是public 修饰的公共方法-- 为了区分,我在 Student类里面放了一个 private 的构造方法 Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { System.out.println( "可以访问到的构造方法:" + constructor.getName() ); } Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors(); for (Constructor<?> constructor : declaredConstructors) { System.out.println( "定义的构造方法:" + constructor ); }

运行效果:

可以访问到的构造方法:com.zzq.reflect.anotation.Student
可以访问到的构造方法:com.zzq.reflect.anotation.Student
定义的构造方法:public com.zzq.reflect.anotation.Student()
定义的构造方法:public com.zzq.reflect.anotation.Student(java.lang.String,java.lang.String,int)
定义的构造方法:private com.zzq.reflect.anotation.Student(java.lang.String,java.lang.String)

动态调用构造方法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Class<?> clazz = Class.forName("com.zzq.reflect.anotation.Student"); // 此处如果 Student类里面没有 无参构造则会报错 Student stu = (Student)clazz.newInstance(); stu.setName("zhangzq"); System.out.println( stu ); // 调用指定的构造方法 Constructor<?> c1 = clazz.getDeclaredConstructor(String.class,String.class,int.class); Student stu1 = (Student)c1.newInstance("zhangzq","清华大学",18); System.out.println( stu1 ); // 调用私有的构造方法, 两个string 的构造方法,我设置成了 私有的 Constructor<?> c2 = clazz.getDeclaredConstructor(String.class,String.class); // 因为是私有的,这里不能方法,所以得设置该方法可以访问,设置访问权限为true // 不设置则会报错 c2.setAccessible(true); Student stu2 = (Student)c2.newInstance("zhangzq","清华大学"); System.out.println( stu2 );

运行效果:

Student [name=zhangzq, school=null, age=0]
Student [name=zhangzq, school=清华大学, age=18]
Student [name=zhangzq, school=清华大学, age=0]
2.获取所有定义字段

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Class<?> clazz = Class.forName("com.zzq.reflect.anotation.Student"); Student stu = (Student)clazz.newInstance(); // 获取可以访问的到的字段 Field[] fields = clazz.getFields(); for (Field field : fields) { System.out.println( "可访问的到的字段:" + field.getName() ); } // 获取 全部定义的字段 Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { System.out.println( "全部定义的字段:" + field.getName() ); } // 获取指定字段 Field f1 = clazz.getDeclaredField("age"); // 设置可以访问 f1.setAccessible(true); // 通过 字段的方法进行set值 f1.set(stu,19); System.out.println( f1.get(stu) );

运行效果:

可访问的到的字段:name
可访问的到的字段:school
可访问的到的字段:age
全部定义的字段:name
全部定义的字段:school
全部定义的字段:age
19

3,获取所有定义的方法,并动态调用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Class<?> clazz = Class.forName("com.zzq.reflect.anotation.Student"); Student stu = (Student)clazz.newInstance(); // 获取所有定义的方法,公共方法一样的套路,参考之前的 Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method method : declaredMethods) { System.out.println( "定义方法:" + method ); } Method setName = clazz.getDeclaredMethod("setName", String.class); Method getName = clazz.getDeclaredMethod("getName", null); // 通过 Method 类的 invoke 方法进行调用该方法 setName.invoke(stu, "zhangzq"); // 动态调用 getName 方法,返回返回值 Object invoke = getName.invoke(stu, null); System.out.println( invoke );

 

运行效果:

定义方法:public java.lang.String com.zzq.reflect.anotation.Student.toString()
定义方法:public java.lang.String com.zzq.reflect.anotation.Student.getName()
定义方法:public void com.zzq.reflect.anotation.Student.setName(java.lang.String)
定义方法:public java.lang.String com.zzq.reflect.anotation.Student.getSchool()
定义方法:public int com.zzq.reflect.anotation.Student.getAge()
定义方法:public void com.zzq.reflect.anotation.Student.setSchool(java.lang.String)
定义方法:public void com.zzq.reflect.anotation.Student.setAge(int)
zhangzq

 

 

最后

以上就是小巧冰淇淋最近收集整理的关于反射之利用反射获取类方法,字段构造方法,并进行操作注意看代码里面的注释的全部内容,更多相关反射之利用反射获取类方法,字段构造方法,并进行操作注意看代码里面内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部