@@ -132,36 +132,35 @@ public class Main {
132
132
*/
133
133
Class<?> tagetClass = Class . forName(" cn.javaguide.TargetObject" );
134
134
TargetObject targetObject = (TargetObject ) tagetClass. newInstance();
135
-
136
135
/**
137
136
* 获取 TargetObject 类中定义的所有方法
138
137
*/
139
- Method [] methods = tagetClass . getDeclaredMethods();
138
+ Method [] methods = targetClass . getDeclaredMethods();
140
139
for (Method method : methods) {
141
140
System . out. println(method. getName());
142
141
}
143
142
144
143
/**
145
144
* 获取指定方法并调用
146
145
*/
147
- Method publicMethod = tagetClass . getDeclaredMethod(" publicMethod" ,
146
+ Method publicMethod = targetClass . getDeclaredMethod(" publicMethod" ,
148
147
String . class);
149
148
150
149
publicMethod. invoke(targetObject, " JavaGuide" );
151
150
152
151
/**
153
152
* 获取指定参数并对参数进行修改
154
153
*/
155
- Field field = tagetClass . getDeclaredField(" value" );
156
- // 为了修改类中的私有字段我们必须取消安全检查
154
+ Field field = targetClass . getDeclaredField(" value" );
155
+ // 为了对类中的参数进行修改我们取消安全检查
157
156
field. setAccessible(true );
158
157
field. set(targetObject, " JavaGuide" );
159
158
160
159
/**
161
160
* 调用 private 方法
162
161
*/
163
- Method privateMethod = tagetClass . getDeclaredMethod(" privateMethod" );
164
- // 为了调用 private 方法我们必须取消安全检查
162
+ Method privateMethod = targetClass . getDeclaredMethod(" privateMethod" );
163
+ // 为了调用private方法我们取消安全检查
165
164
privateMethod. setAccessible(true );
166
165
privateMethod. invoke(targetObject);
167
166
}
@@ -181,6 +180,6 @@ value is JavaGuide
181
180
** 注意** : 有读者提到上面代码运行会抛出 ` ClassNotFoundException ` 异常,具体原因是你没有下面把这段代码的包名替换成自己创建的 ` TargetObject ` 所在的包 。
182
181
183
182
``` java
184
- Class<?> tagetClass = Class . forName(" cn.javaguide.TargetObject" );
183
+ Class<?> targetClass = Class . forName(" cn.javaguide.TargetObject" );
185
184
```
186
185
0 commit comments