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;
}
public static void main(String args[]) {
java.lang.reflect.Field xx = new A().field;
}
}
class A{
private int a =3;
}
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;
}
// 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
}
}
}
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;
}
修改A.a就不可以吗?