As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center作为指导。
已关闭8年。
我听说反射机制破坏了Java的安全性。有人可以解释一下吗?
已关闭8年。
我听说反射机制破坏了Java的安全性。有人可以解释一下吗?
最佳答案
您可以使用反射访问私有字段和对象的方法
例如
public class Foo {
private String privateString = null;
public Foo(String privateString) {
this.privateString = privateString;
}
}
Foo privateObject = new Foo("The Private Value");
Field privateStringField = Foo.class.
getDeclaredField("privateString");
privateStringField.setAccessible(true);
String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);
07-26 05:31