java 根据反射输出私有属性的值

public class Person {
    private String name;

    Person(String c) {
        this.name = c;
    }
}
Person person = new Person("sssss");
Class c = Class.forName("com.example.zong_demo.Person");
Field[] fields = person.getClass().getDeclaredFields();
fields[0].setAccessible(true);
fields[0].get(person);
System.out.println(fields[0].get(person));

你可能感兴趣的:(java)