为什么我在构造器里调用 反射, 取得 field 总是为 null?
构造完毕后可以的,
构造时不能反射吗?
类不是已经有了吗?

解决方案 »

  1.   

    代码如下
    Class me = this.getClass();
    Field[] fields = me.getDeclaredFields();// 得到所有成员变量
    for (Field field : fields)// 增强for循环
    {
    System.out.println("field name is "+field.getName());  //打印出field的name
    System.out.println("field obj is "+ field.get(this));  //打印出 field 得值
    }如果这段代码放在构造器中,无论field 是否设了初始值,打出来都是 null,
    放在非构造器方法中是可以打印出来初始值的.
      

  2.   

    放在非构造器方法中也可以啊,代码如下:
    import java.lang.reflect.*;
    public class Test {
    public static void main(String[] args) throws IllegalAccessException {
    A a=new A();
    /*try {
    a.show();
    }catch(IllegalAccessException iae) {
    iae.printStackTrace();
    }*/
    }
    }class A {
    int i;
    String b;
    public A() throws IllegalAccessException {
    Class me = this.getClass();
    Field[] fields = me.getDeclaredFields();
    for (Field field : fields)
    {
    System.out.println("field name is "+field.getName());
    System.out.println("field obj is "+ field.get(this));
    }

    }
      

  3.   

    大哥,你说的是。。
    我的代码有一层继承关系,为什么加了这层关系就不行了呢????import java.lang.reflect.*;
    public class Test {
    public static void main(String[] args) throws IllegalAccessException {
    byte c[]=null;
    C a=new C();
    /*try {
    a.show();
    }catch(IllegalAccessException iae) {
    iae.printStackTrace();
    }*/
    }
    }
    class C extends A{
    public C()throws IllegalAccessException {
    super();
    }
    byte[] d ={0,0,0,0};
    }
    class A {
    int i;
    //String b="aaa";
    byte c[]={0x23,0x11};
    public A() throws IllegalAccessException {
    Class me = this.getClass();
    Field[] fields = me.getDeclaredFields();
    for (Field field : fields)
    {
    System.out.println("field name is "+field.getName());
    System.out.println("field obj is "+ field.get(this));
    }

    }输出结果:
    field name is d
    field obj is null
      

  4.   

    thank you very very much
      

  5.   

    //这个是孙鑫视频中的一段代码,在jdk1。4下运行没有问题,在jdk1.5下invoke()这个方法
    //使用异常。
    //在通过 cmd >java ClassTest Point 运行这个类,动态产生一个Point类的对象,
    //动态调用这个对象的方法。
    import java.lang.reflect.*;
    import java.lang.*;//这个的导入和上面的导入无关。
    class ClassTest
    {
    public static void main(String[] args)
    {
    try
    {
      Class c = Class.forName(args[0]); 
      //此处要写入你类的全路径+类名
      
      Constructor[] cons = c.getDeclaredConstructors();
      System.out.println(cons[0]);
      Class[] parames = cons[0].getParameterTypes(); 
      //返回一个Class数组;作用为获取构建器[0]中的参数,
      //在Constructor<T>中。
     
      Object[] paramesValue = new Object[parames.length];
      //因为cons[0].newInstance(Object[] o)
      //所以声明了paramsValue这个对象数组。创建一个
      //parames.length大小的对象数组。
      
      for (int i=0;i<parames.length;i++)
      //根据这个构造方法的参数个数。
      {if(parames[i].isPrimitive())
       //判断如果是基本类型
       {
       paramesValue[i] = new Integer(i+5);
       //将这个对象列表中添加int对象。
       System.out.println(paramesValue[i].toString());
       }
      
      }

      Object o = cons[0].newInstance(paramesValue);
      //返回一个对象,通过此方法获取一个[0]号构造器的对象。
      Method[] ms = c.getDeclaredMethods();
      //获取这个构造器的方法名
      System.out.println("MethodName = "+ms[0]);
      //ms[0].invoke(o,0);//这个调用在jdk1.4的时候可以,在1.5的就不行了,

      }catch(Exception ex)
      
      {ex.printStackTrace();}
    }
     }
    class Point
    { static
    {
    System.out.println("static");
    }
    int x,y;

    void output()
    {
    System.out.println("output:-> x="+x+",y="+y);
    }
     Point(int x,int y)
    {
    this.x = x;
    this.y = y;
    }} 
      

  6.   

    echoiori()  
    你的例子中蕴含了什么深意呢?我看不出跟我的问题有什么关系啊?
      

  7.   

    import java.lang.reflect.*;
    public class Test {
    public static void main(String[] args) throws IllegalAccessException {
    byte c[]=null;
    C a=new C();
    /*try {
    a.show();
    }catch(IllegalAccessException iae) {
    iae.printStackTrace();
    }*/
    }
    }
    class C extends A{
    public C()throws IllegalAccessException {
    super();
    }
    byte[] d ={0,0,0,0};
    }
    class A {
    int i;
    //String b="aaa";
    byte c[]={0x23,0x11};
    public A() throws IllegalAccessException {
    Class me = this.getClass();
    Field[] fields = me.getDeclaredFields();
    for (Field field : fields)
    {
    System.out.println("field name is "+field.getName());
    System.out.println("field obj is "+ field.get(this));
    }

    }输出结果:
    field name is d
    field obj is null
    =========================================this.getClass(); 
    这个时候 this是class C的对象  反射出来的 当然只有class c的field
    然后在C的super class A的构造函数中打印  这个时候C当然没有初始化完成
    回去看看继承初始化的过程就知道为什么了
      

  8.   

    o ,把 field 改成 static就行了 ,哦也