是否可以从使用反射中访问protected成员?SubclassSuperClass
private void accessFields() {
Field[] fields = this.getClass().getDeclaredFields();
for(Field field : fields) {
if(Modifier.isProtected(field.getModifiers()) {
//Will this always work? Or will get(this) throw an IllegalAccessException?
Object value = field.get(this);
}
}
}
请注意,这将与普通protected成员访问相反,不是SubClass访问protected成员,而是SuperClass.