一个简单类RModulepublic class RModule extends RObject
{       public volatile int x ;
       public volatile int y ;
       public volatile int w ;
       public volatile int h ;
       public RModule()
       {
       }       public void setX(int x)
       {
              this.x = x ;
       }       public void setY(int y)
       {
              this.y = y ;
       }       public void setWidth(int w)
       {
              this.w = w ;
       }       public void setHeight(int h)
       {
              this.h = h ;
       }       public void setPoint(int x,int y)
       {
              setX(x) ;
              setY(y) ;
       }       public void setSize(int w,int h)
       {
              setWidth(w) ;
              setHeight(h);
       }}取相关定义的变量的方法:       public void testCode()
       {
              RModule module = new RModule() ;
              module.setPoint(30,20);
              module.setSize(40,60);
              
              Field[] fields = module.getClass().getDeclaredFields();
              Field field = fields[0] ;
              
      try
      {
     System.out.println("field 0 value = " +
field.get(field.getName()));
      } catch (IllegalAccessException ex)
      {
                     ex.printStackTrace();
      } catch (IllegalArgumentException ex)
      {
                     ex.printStackTrace();
      }
              
       }testCode方法中,我想通过field的name来取得他的值,也就是取得变量x的值,现在是30.可为什么老是报错呢?
doc中的get方法,说的太绕了,看了半天都不知道是说什么意思:
返回指定对象上此 Field 表示的字段的值。
指定对象应该是我这个例子中的field么?
表示的的字段是field的某个属性,比如name或者是type么?还是
指定对象是Class,表示的字段是field?晕了...doc上也不给例子怎么用,有例子一下就看明白了.

解决方案 »

  1.   


    System.out.println("field 0 value = " + field.get(module));
      

  2.   

    能得到一个属性的名称。但是值得不到。关注。
    import java.lang.reflect.Field;public class RModule {
    // volatile
    public volatile int x;
    public volatile int y;
    public volatile int w;
    public volatile int h; public RModule() {
    } public void setX(int x) {
    this.x = x;
    } public void setY(int y) {
    this.y = y;
    } public void setWidth(int w) {
    this.w = w;
    } public void setHeight(int h) {
    this.h = h;
    } public void setPoint(int x, int y) {
    setX(x);
    setY(y);
    } public void setSize(int w, int h) {
    setWidth(w);
    setHeight(h);
    } public void testCode() { Class c;
    try {
    setX(123) ;
    c = Class.forName("RModule");
    System.out.println("属性:");
    Field f[] = c.getDeclaredFields();
    for (int i = 0; i < f.length; i++) {
    System.out.println(f[i].getName());
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } } public static void main(String args[]) { new RModule().testCode();
    }}
      

  3.   


    public class RModule { public volatile int x;
    public volatile int y;
    public volatile int w;
    public volatile int h; public RModule() {
    } public void setX(int x) {
    this.x = x;
    } public void setY(int y) {
    this.y = y;
    } public void setWidth(int w) {
    this.w = w;
    } public void setHeight(int h) {
    this.h = h;
    } public void setPoint(int x, int y) {
    setX(x);
    setY(y);
    } public void setSize(int w, int h) {
    setWidth(w);
    setHeight(h);
    } public static void testCode() {
    RModule module = new RModule();
    module.setPoint(30, 20);
    module.setSize(40, 60);
    Field[] fields = module.getClass().getDeclaredFields();
    Field field = fields[0];
    try {
    //System.out.println("field 0 value = " + field.get(field.getName()));
    System.out.println("field 0 value = " + field.get(module));
    } catch (IllegalAccessException ex) {
    ex.printStackTrace();
    } catch (IllegalArgumentException ex) {
    ex.printStackTrace();
    }
    } public static void main(String args[]) {
    testCode();
    }}
      

  4.   

    try
              {
                 System.out.println("field 0 value = " +
                        field.get(module));
              } catch (IllegalAccessException ex)