Java 反射是可访问的方法
2022-09-04 01:58:23
我正在学习反思。当我执行以下代码时:
package main;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Base firstBase = new Base();
Field firstBaseField = firstBase.getClass().getDeclaredField("protectedBuffer");
System.out.println(firstBaseField.isAccessible());
}
}
这是基类:
package main;
public class Base {
private StringBuffer buffer;
protected StringBuffer protectedBuffer;
public StringBuffer buffer2;
}
结果为假。但这不是真的,因为我可以以这种方式访问:?protectedBuffer
firstBase.protectedBuffer