JAVA 反射机制 获得 private 变量
- public class Triangle {
- // 定义三角形的三边
- protected long lborderA = 0;
- protected long lborderB = 0;
- protected long lborderC = 0;
- }
获得 protected 三个变量 (private 也一样)
- Triangle triangle = new Triangle(2, 2, 3);
- ry {
- Field title1 = triangle.getClass().getDeclaredField("lborderA");
- title1.setAccessible(true);
- Field title2 = triangle.getClass().getDeclaredField("lborderB");
- title2.setAccessible(true);
- Field title3 = triangle.getClass().getDeclaredField("lborderC");
- title3.setAccessible(true);
- long lborderA = (Long) title1.get(triangle);
- long lborderB = (Long) title2.get(triangle);
- long lborderC = (Long) title3.get(triangle);
posted on 2016-08-17 20:27 nickTimer 阅读(1476) 评论(0) 编辑 收藏 举报