黑马学习-------------反射(访问私有变量)

来源:互联网 发布:e52软件下载 编辑:程序博客网 时间:2024/05/21 19:35

ReflectPoint.java

package com.belmeng.test;public class ReflectPoint{private int x;private int y;   public ReflectPoint(){}public ReflectPoint(int x, int y){this.x = x;this.y = y;}}


ReflectTest.java

package com.belmeng.test;import java.lang.reflect.Field;public class ReflectTest{  public static void main(String[] args) throws Exception{ReflectPoint point=new ReflectPoint(3, 5);Field x=point.getClass().getDeclaredField("x");x.setAccessible(true);System.out.println(x.get(point));*/}}