50,857
社区成员
发帖
与我相关
我的任务
分享
public class People {
private String name = "1";
public People(String name){
this.name = "2";
}
public void setName(String name){
this.name = name;
}
public static voie main(String[] arg){
People people = new People("3");
people.setName("4");
//不管私有公有一律全强
Field field=object.getClass().getDeclaredField("name");
//参数值为true,禁用访问控制检查
field.setAccessible(true);
System.out.println(field.get(object));
}
}
import java.lang.reflect.*;
public class People {
private String name = "1";
public People(String name){
this.name = "2";
}
public void setName(String name){
this.name = name;
}
public static void main(String[] arg) throws Exception {
People people = new People("3");
people.setName("4");
//不管私有公有一律全强
Field field = people.getClass().getDeclaredField("name"); // 你用的是 object,不知道哪里来的,改成了 people
//参数值为true,禁用访问控制检查
field.setAccessible(true);
System.out.println(field.get(people));
}
}
[/quote]
这样不是就跟自己new个然后调用gettersetter一样么.....
除非没gettersetter方法...
import java.lang.reflect.*;
public class People {
private String name = "1";
public People(String name){
this.name = "2";
}
public void setName(String name){
this.name = name;
}
public static void main(String[] arg) throws Exception {
People people = new People("3");
people.setName("4");
//不管私有公有一律全强
Field field = people.getClass().getDeclaredField("name"); // 你用的是 object,不知道哪里来的,改成了 people
//参数值为true,禁用访问控制检查
field.setAccessible(true);
System.out.println(field.get(people));
}
}