首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

反照-运行时变更field内容

2014-01-25 
反射-运行时变更field内容package com.reflectimport java.lang.reflect.Field/** * @Description:运行

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

?

热点排行