反射-运行时变更field内容
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 */}}
?