本帖最后由 杨朔 于 2012-7-4 19:50 编辑
两个程序第一个
public class TheOne {
private String s1="ball";
public String s2="bastball";
TheOne(){
}
}
第二个
import java.lang.reflect.Field;
public class Two {
public static void main(String[] args) throws Exception {
TheOne to=new TheOne();
change(to);
System.out.println(to);
}
public static void change(Object obj) throws Exception{
Field[] field = obj.getClass().getDeclaredFields();
for(Field fi:field){
if(fi.getType()==String.class){
String s1=(String) fi.get(obj);
System.out.println("s1");
}
}
}
}
一直提示java.lang.IllegalAccessException: Class Two can not access a member of class TheOne with modifiers "private"这是为什么 |