50,639
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
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));
}
}