package com.reflect;
import java.lang.reflect.Field;
/**
* @Description: 运行时变更field内容
*/
public class RefFiled {
public double x;
public Double y;
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Class<RefFiled> c = RefFiled.class;
Field xf = c.getField("x");
Field yf = c.getField("y");
RefFiled obj = new RefFiled();
System.out.println("变更前x=" + xf.get(obj));
//变更成员x值
xf.set(obj, 1.1);
System.out.println("变更后x="+xf.get(obj));
System.out.println("变更前y=" +yf.get(obj));
//变更成员y值
yf.set(obj, 2.1);
System.out.println("变更后y=" + yf.get(obj));
/**变更前x=0.0
变更后x=1.1
变更前y=null
变更后y=2.1
*/
}
}