@@ -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