博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
反射的使用
阅读量:4182 次
发布时间:2019-05-26

本文共 4779 字,大约阅读时间需要 15 分钟。

package com.xiaobu.Reflect;import lombok.Data;import java.io.Serializable;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2021/6/15 19:02 * @description */@Datapublic class Person implements Serializable {
private static final long serialVersionUID = 7353364411628218048L; private String name; private int age;}
package com.xiaobu.Reflect;import lombok.Data;import lombok.SneakyThrows;import java.io.Serializable;import java.lang.reflect.Field;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2021/6/15 18:45 * @description */public class ReflectTest {
@SneakyThrows public static void main(String[] args) {
demo2(); getInstance(); demo(); } @SneakyThrows public static void demo() {
Class
c = Person.class; //Class
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.Class System.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)); } } @SneakyThrows public static void demo2() {
Person person = new Person(); //通过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)); } } @SneakyThrows public static void getInstance() {
//1. 通过类名获取 Class
p= com.xiaobu.Reflect.Person.class; //p = class com.xiaobu.Reflect.ReflectTest$Person System.out.println("p = " + p); //2. 通过对象实例获取 com.xiaobu.Reflect.Person person = new com.xiaobu.Reflect.Person(); Class
personClass = person.getClass(); System.out.println(personClass); //3. 通过Class类静态方法获取 //通过Class类静态方法forName()获取 //Exception in thread "main" java.lang.ClassNotFoundException: com.xiaobu.Reflect.ReflectTest.Person Class
c = Class.forName("com.xiaobu.Reflect.ReflectTest.Person"); System.out.println(c); } @Data public static class Person implements Serializable {
private static final long serialVersionUID = 7353364411628218048L; private String name; private int age; }}
package com.xiaobu.Reflect;import lombok.SneakyThrows;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2021/6/15 18:45 * @description */public class ReflectTest2 {
@SneakyThrows public static void main(String[] args) {
getInstance(); } @SneakyThrows public static void getInstance() {
//1. 通过类名获取 Class
p=Person.class; //p = class com.xiaobu.Reflect.ReflectTest$Person System.out.println("p = " + p); //2. 通过对象实例获取 Person person = new Person(); Class
personClass = person.getClass(); System.out.println(personClass); //3. 通过Class类静态方法获取 //通过Class类静态方法forName()获取 Class
c = Class.forName("com.xiaobu.Reflect.Person"); System.out.println(c); }}
  1. 可以看出 static的类无法通过Class.forName(“com.xiaobu.Reflect.Person”)来加载出来
  2. 类.class 无法给类的属性赋值以及获取值

转载地址:http://mkgai.baihongyu.com/

你可能感兴趣的文章
覆盖equals方法时总是要覆盖hashCode
查看>>
clone详解
查看>>
【Java并发编程实战】——AbstractQueuedSynchronizer源码分析(一)
查看>>
【Java并发编程实战】——并发编程基础
查看>>
【Java并发编程实战】——Java内存模型与线程
查看>>
Java复制文件的4种方式
查看>>
mysql的JDBC连接工具类
查看>>
利用多线程(用到原子类AtomicInteger)往数据库批量插入大量数据
查看>>
多个线程操作数组
查看>>
定长线程池的应用
查看>>
ArrayBlockingQueue的简单使用
查看>>
Git 常用命令总结(一)
查看>>
Git 常用命令总结(二)
查看>>
JAVA 并发——synchronized的分析
查看>>
Echarts——使用 dataset 管理数据
查看>>
DES 加解密工具类
查看>>
SpringBoot多模块项目实践(Multi-Module)
查看>>
第一篇: 服务的注册与发现Eureka(Greenwich版)
查看>>
第二篇: 服务消费者(rest+ribbon)(Greenwich版本)
查看>>
第三篇: 服务消费者(Feign)(Greenwich版本)
查看>>