1、Class对象获取成员变量:
Class类的常用方法(获取Field对象):
创建Person类:
package pers.reflect.person; public class Person { private String name; private int age; public String hobby; public String height; protected String sex; String address; public Person(){ } public Person(String name,int age){ this.name=name; this.age=age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", hobby=" + hobby + ", height=" + height + ", sex=" + sex + ", address=" + address + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import java.lang.reflect.Field; import pers.reflect.person.Person; public class ReflectDemo { public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Class c = Person.class; System.out.println("getFields(),获取公共的成员变量:"); Field[] fields = c.getFields(); for (Field field : fields) { System.out.println(field); } System.out.println("getField(),获取指定的公共的成员变量:"); Field hobbyField = c.getField("hobby"); System.out.println(hobbyField); System.out.println("getDeclaredFields(),获取所有的成员变量,与修饰符无关。"); Field[] fields1 = c.getDeclaredFields(); for (Field field : fields1) { System.out.println(field); } System.out.println("获取私有的指定的成员变量"); Field nameField = c.getDeclaredField("age"); System.out.println(nameField); } }
2、Field类的常用方法:
(1)返回变量的类型:
(2)返回属性的名字:
import java.lang.reflect.Field; import pers.reflect.person.Person; public class ReflectDemo1 { public static void main(String[] args) { Class c = Person.class; System.out.println("getDeclaredFields(),获取所有的成员变量,与修饰符无关。"); System.out.println(); Field[] fields1 = c.getDeclaredFields(); for (Field field : fields1) { System.out.println("成员变量:"+field); System.out.println("属性的类型:"+field.getType()); System.out.println("属性的名字:"+field.getName()); System.out.println(); } } }
import java.lang.reflect.Field; import pers.reflect.person.Person; public class ReflectDemo2 { public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Class c = Person.class; System.out.println("getField(),获取指定的公共的成员变量:"); Field hobbyField = c.getField("hobby"); System.out.println(hobbyField); Person p=new Person(); Object value=hobbyField.get(p); System.out.println(value); hobbyField.set(p, "张可可"); System.out.println(p); System.out.println(); } }
访问不是被public修饰的成员变量(要用暴力反射,否者无法访问):
import java.lang.reflect.Field; import pers.reflect.person.Person; public class ReflectDemo3 { public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Class c = Person.class; Field nameField = c.getDeclaredField("name"); nameField.setAccessible(true);//暴力反射 System.out.println(nameField); Person p = new Person(); Object value = nameField.get(p); System.out.println(value); nameField.set(p, "赵晓霞"); System.out.println(p); } }
setAccessible(true);忽略权限修饰符的安全检查。
转载于:https://www.cnblogs.com/zhai1997/p/11390314.html