Java反射之剖析方法

时间:2021-11-18 21:26:41

继上篇反射构造函数后,今剖析函数方法。

  要通过反射执行方法:

  1、可以先反射出类或构造函数,创建实例对象,通过对象调用方法(反射通过构造函数创建对象参见上篇)。

  2、可以通过类反射出方法,传入关联对象,从而实现对象调用方法。

 

以下是一个简单测试类:

包含两个文件:

  1、Person.java,用于反射操作

  2、Deom:Demo_MtdRft.java,反射的测试代码

测试的情况包括:1、公有方法,2、私有方法,3、无参数方法,4、有参数方法,5静态方法

下面是两个测试代码文件:

Person.java

Java反射之剖析方法Java反射之剖析方法
  1 package cn.rt.gwq;
2
3 import java.util.List;
4
5 public class Person {
6
7 public String value = "public value";
8
9 /**
10 *无参数构造函数
11 */
12 public Person() {
13 System.out.println("no params constructor");
14 }
15
16 /**
17 * 一个参数的构造函数
18 * @param name
19 */
20 public Person(String name) {
21 System.out.println("string name constructor");
22 value = "change the value : "+name;
23 }
24
25 /**
26 * 一个参数的构造函数
27 * @param age
28 */
29 public Person(int age) {
30 System.out.println("int age constructor");
31 value = "change the value : "+age;
32 }
33
34 /**
35 * 多个参数的构造函数
36 * @param usm
37 * @param pwd
38 */
39 public Person(String usm,String pwd) {
40 System.out.println("String usm,String pwd constructor");
41 value = "change the value : "+usm +":"+pwd;
42 }
43
44 /**
45 * 私有的构造函数
46 * @param list
47 */
48 private Person(List list) {
49 System.out.println("List constructor");
50 value = "change the value : "+list.toArray().toString();
51 }
52 ////////
53
54
55
56
57
58
59
60
61 ////////
62 /**
63 *无参数方法
64 */
65 public void eat(){
66 System.out.println("eating soup...");
67 }
68
69 /**
70 * 有参数方法
71 * @param food
72 */
73 public void eat(String food){
74 System.out.println(food + "is delicious...");
75 }
76
77 /**
78 * 有返回值的方法
79 * @param food
80 * @return
81 */
82 public String eat(String name,String food){
83 System.out.println(food + "is good,"+name+", try it.");
84 return food;
85 }
86
87 /**
88 * 私有方法
89 */
90 private void work(){
91 System.out.println("I don't like to work.");
92 }
93
94 /**
95 * 静态方法
96 */
97 public static void sleep(){
98 System.out.println("I'm sleeping ....");
99 }
100
101
102
103 }
Person.java

Demo_MtdRft.java

Java反射之剖析方法Java反射之剖析方法
  1 package cn.rt.gwq;
2
3 import java.lang.reflect.Method;
4
5 import org.junit.Test;
6
7 /**
8 * 反射方法的测试demo
9 * @author Administrator
10 *
11 */
12 public class Demo_MtdRft {
13
14 /**
15 * 反射没有参数的公开方法
16 * @throws Exception
17 */
18 @Test
19 public void test1() throws Exception{
20
21 Person person = new Person();
22 //加载类
23 Class clazz = Class.forName("cn.rt.gwq.Person");
24 //获取方法
25 Method method = clazz.getMethod("eat", null);
26 //执行方法
27 method.invoke(person, null);
28
29 return;
30
31 }
32
33 /***
34 * 反射带一个参数的方法
35 * @throws Exception
36 */
37 @Test
38 public void test2() throws Exception{
39
40 Person person = new Person();
41
42 Class clazz = Class.forName("cn.rt.gwq.Person");
43 Method method = clazz.getMethod("eat", String.class);
44 method.invoke(person, "fish");
45
46 return;
47
48 }
49
50 /***
51 * 反射有返回值的方法
52 * @throws Exception
53 */
54 @Test
55 public void test3() throws Exception{
56
57 Person person = new Person();
58
59 Class clazz = Class.forName("cn.rt.gwq.Person");
60 Method method = clazz.getMethod("eat", String.class,String.class);
61 String food = (String) method.invoke(person, "tom","fish");
62 System.out.println("food :"+food);
63
64 return;
65
66 }
67
68 /**
69 * 反射私有方法
70 * @throws Exception
71 */
72 @Test
73 public void test4() throws Exception{
74
75 Person person = new Person();
76
77 Class clazz = Class.forName("cn.rt.gwq.Person");
78 //获取私有方法
79 Method method = clazz.getDeclaredMethod("work", null);
80 //强制在类外可自执行
81 method.setAccessible(true);
82 //执行
83 method.invoke(person, null);
84
85 return;
86
87 }
88
89 /**
90 * 反射静态方法
91 * @throws Exception
92 */
93 @Test
94 public void test5() throws Exception{
95
96 Person person = new Person();
97
98 Class clazz = Class.forName("cn.rt.gwq.Person");
99 //也可以用getMethod方法获取
100 Method method = clazz.getDeclaredMethod("sleep", null);
101 method.setAccessible(true);
102 method.invoke(person, null);
103
104 return;
105
106 }
107
108
109 }
Demo_MtdRft.java