【发布时间】:2014-02-05 17:50:15
【问题描述】:
谁能告诉我应该在 String 类 API 中进行更改(我知道我们不能这样做),以便下面的程序应该打印“Earth”?或
我们怎样才能停止打印“太阳”?如何停止黑客攻击?
public class StringAPI {
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("Earth", "Sun".toCharArray());
System.out.println("Earth"); // prints "Sun"
}
}
【问题讨论】:
-
你认为答案是什么?这是面试题吗?如果我们做对了,我们能得到这份工作吗?
-
"如何停止黑客攻击?"第一步是承认你有问题......
-
请参阅 this question 以获取
SecurityManager解决方案。
标签: java string reflection immutability