classPerson{ private String name; privateint age; protectedint weight; int height; public String nickname; public String hobby; // 省略构造方法和成员方法 }
publicclassReflectDemo{ publicstaticvoidmain(String[] args)throws Exception { Class personClass = Person.class; // 1.获取权限为public的指定成员变量 Field nickname = personClass.getField("nickname"); System.out.println(nickname); // 2.获取所有权限为public的成员变量 Field[] fields = personClass.getFields(); for (Field field : fields) { System.out.println(field); } // 3.获取指定的成员变量 Field age = personClass.getDeclaredField("age"); System.out.println(age); // 4.获取所有的成员变量 Field[] declaredFields = personClass.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); } } }
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
public java.lang.String my.study.reflect.Person.nickname -------------------------------------- public java.lang.String my.study.reflect.Person.nickname public java.lang.String my.study.reflect.Person.hobby -------------------------------------- private int my.study.reflect.Person.age -------------------------------------- private java.lang.String my.study.reflect.Person.name private int my.study.reflect.Person.age protected int my.study.reflect.Person.weight int my.study.reflect.Person.height public java.lang.String my.study.reflect.Person.nickname public java.lang.String my.study.reflect.Person.hobby
public void my.study.reflect.Person.eat() -------------------------------------- public void my.study.reflect.Person.eat() public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() -------------------------------------- private void my.study.reflect.Person.sleep() -------------------------------------- protected void my.study.reflect.Person.run() void my.study.reflect.Person.read() private void my.study.reflect.Person.sleep() public void my.study.reflect.Person.eat()
publicclassReflectDemo{ publicstaticvoidmain(String[] args)throws Exception { Person person = new Person("Frank", 20); Class personClass = person.getClass(); System.out.println("初始的Person对象: " + person.toString()); // 获取权限为private的成员变量age Field age = personClass.getDeclaredField("age"); // 修改age的值,需要传入实例化的对象和要设置的值 age.set(person, 21); System.out.println("修改后的Person对象: " + person.toString()); } }
运行结果:
1 2
初始的Person对象: Person{name='Frank', age=20, weight=0, height=0, nickname='null', hobby='null'} Exception in thread "main" java.lang.IllegalAccessException: Class my.study.reflect.ReflectDemo can not access a member of class my.study.reflect.Person with modifiers "private"