Skip to content

Commit 4789216

Browse files
authored
Merge pull request #1560 from samho2008/fix-typo-reflection
fix typo: taget -> target in reflection.md
2 parents 18a6e6a + a27ad3d commit 4789216

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

docs/java/basis/reflection.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,35 @@ public class Main {
132132
*/
133133
Class<?> tagetClass = Class.forName("cn.javaguide.TargetObject");
134134
TargetObject targetObject = (TargetObject) tagetClass.newInstance();
135-
136135
/**
137136
* 获取 TargetObject 类中定义的所有方法
138137
*/
139-
Method[] methods = tagetClass.getDeclaredMethods();
138+
Method[] methods = targetClass.getDeclaredMethods();
140139
for (Method method : methods) {
141140
System.out.println(method.getName());
142141
}
143142

144143
/**
145144
* 获取指定方法并调用
146145
*/
147-
Method publicMethod = tagetClass.getDeclaredMethod("publicMethod",
146+
Method publicMethod = targetClass.getDeclaredMethod("publicMethod",
148147
String.class);
149148

150149
publicMethod.invoke(targetObject, "JavaGuide");
151150

152151
/**
153152
* 获取指定参数并对参数进行修改
154153
*/
155-
Field field = tagetClass.getDeclaredField("value");
156-
// 为了修改类中的私有字段我们必须取消安全检查
154+
Field field = targetClass.getDeclaredField("value");
155+
//为了对类中的参数进行修改我们取消安全检查
157156
field.setAccessible(true);
158157
field.set(targetObject, "JavaGuide");
159158

160159
/**
161160
* 调用 private 方法
162161
*/
163-
Method privateMethod = tagetClass.getDeclaredMethod("privateMethod");
164-
// 为了调用 private 方法我们必须取消安全检查
162+
Method privateMethod = targetClass.getDeclaredMethod("privateMethod");
163+
//为了调用private方法我们取消安全检查
165164
privateMethod.setAccessible(true);
166165
privateMethod.invoke(targetObject);
167166
}
@@ -181,6 +180,6 @@ value is JavaGuide
181180
**注意** : 有读者提到上面代码运行会抛出 `ClassNotFoundException` 异常,具体原因是你没有下面把这段代码的包名替换成自己创建的 `TargetObject` 所在的包 。
182181

183182
```java
184-
Class<?> tagetClass = Class.forName("cn.javaguide.TargetObject");
183+
Class<?> targetClass = Class.forName("cn.javaguide.TargetObject");
185184
```
186185

0 commit comments

Comments
 (0)