packagecom.xiaobu.Reflect;importlombok.Data;importlombok.SneakyThrows;importjava.io.Serializable;importjava.lang.reflect.Field;/**
* @author xiaobu
* @version JDK1.8.0_171
* @date on 2021/6/15 18:45
* @description
*/publicclassReflectTest{@SneakyThrowspublicstaticvoidmain(String[] args){demo2();getInstance();demo();}@SneakyThrowspublicstaticvoiddemo(){Class<Person> c =Person.class;//Class<? extends Person> c = Person.class;Field f1 = c.getDeclaredField("name");System.out.println("-----Class.getDeclaredField(String name)用法-------");System.out.println("-----Class.getDeclaredFields()用法-------");//通过Class.getDeclaredFields()获取类或接口的指定属性值。Field[] f2 = c.getDeclaredFields();for(Field field : f2){
field.setAccessible(true);//Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field com.xiaobu.Reflect.ReflectTest$Person.name to java.lang.ClassSystem.out.println(field.get(c));}//修改属性值System.out.println("----修改name属性------");
f1.setAccessible(true);//Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field com.xiaobu.Reflect.ReflectTest$Person.name to java.lang.Class
f1.set(c,"Maoge");//修改后再遍历各属性的值Field[] f3 = c.getDeclaredFields();for(Field fields : f3){
fields.setAccessible(true);System.out.println(fields.get(c));}}@SneakyThrowspublicstaticvoiddemo2(){Person person =newPerson();//通过Class.getDeclaredField(String name)获取类或接口的指定属性值。Field f1 =Person.class.getDeclaredField("name");//Field f1 = person.getClass().getDeclaredField("name");System.out.println("-----Class.getDeclaredField(String name)用法-------");//System.out.println(f1.get(person));System.out.println("-----Class.getDeclaredFields()用法-------");//通过Class.getDeclaredFields()获取类或接口的指定属性值。Field[] f2 = person.getClass().getDeclaredFields();for(Field field : f2){
field.setAccessible(true);System.out.println(field.get(person));}//修改属性值System.out.println("----修改name属性------");
f1.setAccessible(true);
f1.set(person,"Maoge");//修改后再遍历各属性的值Field[] f3 = person.getClass().getDeclaredFields();for(Field fields : f3){
fields.setAccessible(true);System.out.println(fields.get(person));}}@SneakyThrowspublicstaticvoidgetInstance(){//1. 通过类名获取Class<com.xiaobu.Reflect.Person> p=com.xiaobu.Reflect.Person.class;//p = class com.xiaobu.Reflect.ReflectTest$PersonSystem.out.println("p = "+ p);//2. 通过对象实例获取com.xiaobu.Reflect.Person person =newcom.xiaobu.Reflect.Person();Class<?extendscom.xiaobu.Reflect.Person> personClass = person.getClass();System.out.println(personClass);//3. 通过Class类静态方法获取//通过Class类静态方法forName()获取//Exception in thread "main" java.lang.ClassNotFoundException: com.xiaobu.Reflect.ReflectTest.PersonClass<?> c =Class.forName("com.xiaobu.Reflect.ReflectTest.Person");System.out.println(c);}@DatapublicstaticclassPersonimplementsSerializable{privatestaticfinallong serialVersionUID =7353364411628218048L;privateString name;privateint age;}}
packagecom.xiaobu.Reflect;importlombok.SneakyThrows;/**
* @author xiaobu
* @version JDK1.8.0_171
* @date on 2021/6/15 18:45
* @description
*/publicclassReflectTest2{@SneakyThrowspublicstaticvoidmain(String[] args){getInstance();}@SneakyThrowspublicstaticvoidgetInstance(){//1. 通过类名获取Class<Person> p=Person.class;//p = class com.xiaobu.Reflect.ReflectTest$PersonSystem.out.println("p = "+ p);//2. 通过对象实例获取Person person =newPerson();Class<?extendsPerson> personClass = person.getClass();System.out.println(personClass);//3. 通过Class类静态方法获取//通过Class类静态方法forName()获取Class<?> c =Class.forName("com.xiaobu.Reflect.Person");System.out.println(c);}}