问: 怎么改变final属性?
public class Test {
private A a = new A();
public void test() throws Exception{
Field field = a.getClass().getDeclaredField("finalStr");
field.setAccessible(true);
field.set(a, "bbb");
System.out.println(a.getString());
System.out.println(a.finalStr);
}
public static void main(String[] args) throws Exception{
new Test().test();
}
}
class A{
public final String finalStr = "aaa";
public String getString(){
return finalStr;
}
}
getDeclaredField方法与getField方法
getField方式只能取得public域的属性