0%

重温Java反射机制

本文主要是为了实战Java反射,简单工厂只是一个背景,故对简单工厂模式不做解释了。


1、首先先做个一个使用了简单工厂模式的demo。
Fruit是一个接口,里面含有一个get()方法

1
2
3
4
5
package SimpleFactory;

public interface Fruit {
public void get();
}

2、Apple与Banana为两个实现了Fruit的接口,其中Apple类中方法较多用于实现Java反射中的各种情况。

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
package SimpleFactory;

public class Apple implements Fruit {

private String name = "name";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public void get() {
System.out.println("This is apple");
}
//无参无返回值情况
private void aha(){
System.out.println("This is apple, but you can't see this word." + name);
}

//有参无返回值情况
private void ahaString(String str){
System.out.println("This is apple, but you can't see this word." + str);
}

//有参有返回值情况
private String ahaReturn(String str){
System.out.println("This is apple, but you can't see this word." + str);
return "yes";
}
}

3、FruitFactory为简单工厂模式创建类(为了后期再Main函数中体现Java反射机制,正确的应该像注释中那样,直接返回类)

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
package SimpleFactory;

public class FruitFactory {
public static Class getFruit(String typeName){
try{
//通过字符串获取相关的类并返回
return Class.forName(typeName);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
return null;
}
}

//package SimpleFactory;
//
//public class FruitFactory {
// public static Fruit getFruit(String typeName){
// Class fruitClass = null;
// try {
// fruitClass = Class.forName(typeName);
// return (Fruit) fruitClass.newInstance();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (InstantiationException e) {
// e.printStackTrace();
// }
// return null;
// }
//}

4、下面是执行一下常见的反射操作

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package SimpleFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainSimpleFactory {
public static void main(String[] args) {
//反射机制
try {
//获取Apple的Class
Class appleClass = FruitFactory.getFruit("SimpleFactory.Apple");
//创建实例
Fruit apple = (Fruit) appleClass.newInstance();
//执行实例的public方法
apple.get();
//获取private方法
Method getAha = appleClass.getDeclaredMethod("aha");
//设置Accessible否则无法执行
getAha.setAccessible(true);
//执行该无参无返回值的方法
getAha.invoke(apple);
//获取private属性
Field name = appleClass.getDeclaredField("name");
//设置Accessible否则无法执行
name.setAccessible(true);
//修改该name的值
name.set(apple, "newName");
//修改后再一次执行getAha方法
getAha.invoke(apple);
//获取private方法
Method getAhaString = appleClass.getDeclaredMethod("ahaString",String.class);
//设置Accessible否则无法执行
getAhaString.setAccessible(true);
//执行该有参无返回值的方法
getAhaString.invoke(apple, "false");
//获取private方法
Method getAhaReturn = appleClass.getDeclaredMethod("ahaReturn",String.class);
//设置Accessible否则无法执行
getAhaReturn.setAccessible(true);
//执行该有参有返回值的方法
String res = (String) getAhaReturn.invoke(apple, "true");
System.out.println(res);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}

//与上面相同,只不过是使用简单工厂创建了Banana类的对象
try {
Class appleClass = FruitFactory.getFruit("SimpleFactory.Banana");
Fruit banana = (Fruit) appleClass.newInstance();
banana.get();
Method getAha = appleClass.getDeclaredMethod("ahaa");
getAha.setAccessible(true);
getAha.invoke(banana);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

5、执行结果如下:

1
2
3
4
5
6
7
8
This is apple
This is apple, but you can't see this word.name
This is apple, but you can't see this word.newName
This is apple, but you can't see this word.false
This is apple, but you can't see this word.true
yes
This is Banana
This is banana, but you can't see this word.