如何通过反射修改私有属性的值,附代码

在Java中使用反射修改私有属性

Integer a = Integer.valueOf(1);
// TODO
Field f = a.getClass().getDeclaredField("value");
f.setAccessible(true);
f.set(a,2);
System.out.println(a.intValue()); // 输出2

 参考链接

https://blog.csdn.net/tabactivity/article/details/50726353

posted @ 2019-04-24 17:10  CN-无忧  阅读(1873)  评论(0编辑  收藏  举报