黑马程序员技术交流社区

标题: 利用反射获取对象的变量,我这个代码运行部出来,有什么么问题? [打印本页]

作者: hrfhwy    时间: 2015-12-15 22:39
标题: 利用反射获取对象的变量,我这个代码运行部出来,有什么么问题?
  1. package day23;

  2. import java.lang.reflect.Field;

  3. public class IntegerArraylistAddString {
  4.         public static void main(String[] args)throws Exception {
  5.                 GetSum gs=new GetSum(2,3);
  6.                 Field field1=gs.getClass().getField("y");
  7.                 int mm=(int)field1.get(gs);
  8.                 System.out.println(mm);
  9.                 //Field field2=gs.getClass().getDeclaredField("x");
  10.                 //field2.setAccessible(true);
  11.                
  12.                 //System.out.println(field2.get(gs));
  13.                
  14.                
  15.         }

  16. }
  17. class GetSum
  18. {
  19.          int x;
  20.          int y;
  21.         public GetSum(int x, int y) {
  22.                 super();
  23.                 this.x = x;
  24.                 this.y = y;
  25.         }
  26.        
  27. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2