遇到这样一个业务场景:要使用的类有个私有的变量,并且要使用的该类中一个方法需要用到这个变量。要求在运行时对这个变量进行更改后,使用这个方法,就是要使用这个方法中已经动态改变了的变量。
结合下面的代码,就是在InvokerForDaughter中调用Father的execute()方法,此方法中用到的privateKey是个private的,没有set方法对其进行改变,只有个可利用的构造函数可将其作为参数。而我们又必须在使用execute()方法前将privateKey设置成一个新的值。
public class Father {
private String privateKey;
public Father (String privateKey) {
this.privateKey = privateKey;
}
public void execute(){
System.out.print("execute in Father.");
System.out.print("Father's privateKey="+privateKey);
}
}
public class Daughter extends Father{
public String privateKey;
public Daughter(String privateKey) {
super(privateKey);
try {
Field field = Class.forName("com.exercise.thread.Father").getDeclaredField("privateKey");
field.setAccessible(true);
field.set(this, "father");
// Field field2 = Class.forName("com.exercise.thread.Daughter").getDeclaredField("privateKey");
// field2.setAccessible(true);
// field2.set(this, "father");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class InvokerOfDaughter{
private Daughter daughter;
public InvokerOfDaughter(Daughter daughter) {
this.daughter = daughter;
}
public static void main(String[] args) {
Daughter daughter = new Daughter("daughter");
InvokerOfDaughter InvokerOfDaughter = new InvokerOfDaughter(daughter);
daughter.execute();
}
}
可以看到通过反射的方式,输出的结果已经变成了:
execute in Father.Father's privateKey=father
如果是运行注释掉的三行代码,输出的结果就是:
execute in Father.Father's privateKey=daughter
我们看到正是通过短短的三行代码:Field field = Class.forName("com.exercise.thread.Father").getDeclaredField("privateKey");field.setAccessible(true);field.set(this, "father");就将privateKey这个private变量的值在运行时改变了。