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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
ReflectDemo:

package com.fluffysponge;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;

/**
* 反射获取构造方法并使用练习1:
* 通过反射实现如下的操作:
* Student s = new Student("林青霞",30,"西安");
* System.out.println(s);
*
* 反射获取构造方法并使用练习2:
* 通过反射实现如下的操作:
* Student s = new Student("林青霞");
* System.out.println(s);
* --------------------------------------------------------
* 反射获取成员变量并使用练习:
* 通过反射实现如下的操作:
* Student s = new Student("");
* s.name = "林青霞";
* s.age = 30;
* s.address = "西安";
* System.out.println(s);
* ---------------------------------------------------------
* 反射获取成员方法并使用练习:
* 通过反射实现如下的操作:
* Student s = new Student("");
* s.method1();
* s.method2("林青霞");
* String ss = s.method3("林青霞",30);
* System.out.println(ss);
* s.function();
* ----------------------------------------------------------
* 反射练习:越过泛型检查
* 我有一个ArrayList<Integer>集合,现在想在这个集合中添加一个字符串数据,如何实现?
*
* 反射练习:通过配置文件运行类中的方法
*/


public class ReflectDemo {
// //反射获取构造方法并使用练习1:
// public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// //获取Class对象
// Class<?> c = Class.forName("com.fluffysponge.Student");
//
// //public Student(String name, int age, String address)
// //Constructor<T> getConstructor(Class<?>... parameterTypes)
// Constructor<?> con = c.getConstructor(String.class, int.class, String.class);
// //基本数据类型也可以通过.class得到对应的class类型
//
// //T newInstance(Object... initargs)
// Object obj = con.newInstance("林青霞", 30, "西安");
// System.out.println(obj);
// }

// //反射获取构造方法并使用练习2:
// public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// //获取Class对象
// Class<?> c = Class.forName("com.fluffysponge.Student");
//
// //private Student(String name)
// //Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
// Constructor<?> con = c.getDeclaredConstructor(String.class);
//
// //暴力反射,私有的构造方法不能常规newInstance,需要取消访问检查
// //public void setAccessible(boolean flag):值为true,取消访问检查
// con.setAccessible(true);
//
// Object obj = con.newInstance("林青霞");
// System.out.println(obj);
// }

// //反射获取成员变量并使用练习:
// public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
// //获取Class对象
// Class<?> c = Class.forName("com.fluffysponge.Student");
//
// //Student s = new Student("");
// Constructor<?> con = c.getConstructor();
// Object obj = con.newInstance();
// System.out.println(obj);
//
// //s.name = "林青霞";
// //Field nameField = c.getField("name");//NoSuchFieldException
// Field nameField = c.getDeclaredField("name");//IllegalAccessException
// nameField.setAccessible(true);
// nameField.set(obj,"林青霞");
// System.out.println(obj);
//
// //s.age = 30;
// Field ageField = c.getDeclaredField("age");
// ageField.setAccessible(true);
// ageField.set(obj,30);
// System.out.println(obj);
//
// //s.address = "西安";
// Field addressField = c.getDeclaredField("address");
// addressField.setAccessible(true);
// addressField.set(obj,"西安");
// System.out.println(obj);
// }

// //反射获取成员方法并使用练习:
// public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// //获取Class对象
// Class<?> c = Class.forName("com.fluffysponge.Student");
//
// //Student s = new Student("");
// Constructor<?> con = c.getConstructor();
// Object obj = con.newInstance();
//
// //s.method1();
// Method m1 = c.getMethod("method1");
// m1.invoke(obj);
//
// //s.method2("林青霞");
// Method m2 = c.getMethod("method2", String.class);
// m2.invoke(obj,"林青霞");
//
// //String ss = s.method3("林青霞",30);
// //System.out.println(ss);
// Method m3 = c.getMethod("method3", String.class, int.class);
// Object o = m3.invoke(obj, "林青霞", 30);
// System.out.println(o);
//
// //s.function();
// //Method m4 = c.getMethod("function");//NoSuchMethodException
// Method m4 = c.getDeclaredMethod("function");
// m4.setAccessible(true);
// m4.invoke(obj);
// }

// //反射练习:越过泛型检查
// public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// //创建集合
// ArrayList<Integer> array = new ArrayList<>();
//
//// array.add(10);
//// array.add(20);
//// array.add("hello");
//
// Class<? extends ArrayList> c = array.getClass();
// Method m = c.getMethod("add", Object.class);
// m.invoke(array, "hello");
// m.invoke(array, "world");
// m.invoke(array, "java");
//
// System.out.println(array);
// }

//反射练习:通过配置文件运行类中的方法
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//// Cat cat = new Cat();
//// cat.miaomiao();
//
// Dog dog = new Dog();
// dog.wangwang();

/**
* 具体运行那个文件,在class.txt文件中配置
* className=xxx
* methodName=xxx
*/
//加载数据
Properties prop = new Properties();
FileReader fr = new FileReader("MyReflect\\class.txt");
prop.load(fr);
fr.close();
/*
* className=com.fluffysponge.Cat
methodName=miaomiao
* */
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");

//通过反射来使用
Class<?> c = Class.forName(className);//com.fluffysponge.Cat
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();

Method m = c.getMethod(methodName);//miaomiao
m.invoke(obj);
}
}
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
Student:

package com.fluffysponge;

public class Student {
//成员变量:一个私有,一个默认,一个公共
private String name;
int age;
public String address;

//构造方法:一个私有,一个默认,两个公共

public Student() {
}

private Student(String name) {
this.name = name;
}

Student(String name, int age) {
this.name = name;
this.age = age;
}

public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}

//成员方法:一个私有,四个公共
private void function(){
System.out.println("function");
}

public void method1(){
System.out.println("method");
}

public void method2(String s){
System.out.println("method:" + s);
}

public String method3(String s,int i){
return s + "," + i;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
1
2
3
4
5
6
7
8
9
Cat:

package com.fluffysponge;

public class Cat {
public void miaomiao(){
System.out.println("小猫喵喵叫");
}
}
1
2
3
4
5
6
7
8
9
Dog:

package com.fluffysponge;

public class Dog {
public void wangwang(){
System.out.println("小狗汪汪叫");
}
}
1
2
3
4
class.txt:

className=com.fluffysponge.Cat
methodName=miaomiao