import java.lang.reflect.Field;public class Shuz {
    public static void main(String args[]) {
       java.lang.reflect.Field xx = new A().field;
       
   }
}
class A{
   private int a =3;
}

解决方案 »

  1.   


    import java.lang.reflect.Field;public class TestReflect {
        public static void main(String args[]) {
           java.lang.reflect.Field[] xx = A.class.getDeclaredFields();
           for(int i=0;i<xx.length;i++){
    System.out.println(xx[i]);
       }
       }
    }
    class A{
       private int a =3;
       private int b = 5;
    }
      

  2.   

    好象不对,我要返回 new A()之后的field,并且要达到修改 private int a=3;的目的。
      

  3.   

    所有的field都调用setAccessible(true)就可以了xx.setAccessible(true);;;
      

  4.   

    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
    // TODO Auto-generated method stub
    A oa = new A();
    Field[] fields = A.class.getDeclaredFields();
    for(Field temp : fields ){
    if(temp.getName() == "a"){
    temp.setAccessible(true);
    System.out.println(temp.getInt(oa));//此时为3
    temp.setInt(oa, 5);
    System.out.println(temp.getInt(oa));//此时为5
    }
    }

    }
      

  5.   


    import java.lang.reflect.Field;public class TestReflect { public static void main(String[] args){
    try{
    Class c = Class.forName("A");
    Object tobj = c.newInstance(); Field afld = c.getDeclaredField("a");
    afld.setAccessible(true);
    afld.setInt(tobj,6);
    System.out.println(afld.getInt(tobj));
    }catch(Exception ee){
    ee.printStackTrace();
    }
    }
    }
    class A{
       private int a =3;
       private int b = 5;
    }
      

  6.   

    对于 A xx = new A();
    修改A.a就不可以吗?