public class Monday {
public static void main(String args[]) {
// 【place holder】
System.out.println("aaaa");
}
}
问题:在括号内加几行代码,让打印aaaa那行打印出bbbb
方案1:
public class Monday {
public static void main(String args[]) {
try {
String aaaa = "aaaa";
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
char[] value = (char[]) field.get(aaaa);
Arrays.fill(value, 'b');
} catch (Exception e) {
}
System.out.println("aaaa");
}
}
方案2:
public class Monday {
public static void main(String args[]) {
System.setOut(new PrintStream(System.out) {
public void println(String str) {
super.println("bbb");
}
});
System.out.println("aaa");
}
}
方案1是修改reference内容,方案2是重载。