返璞归真-反射(01)
Java Reflection
一 、基本概念
1. 反射机制允许程序在执行期借助于Reflection API 取得任何类的内部信息(比如成员变量,构造器,成员方法等等),并能操作对象的属性及方法。反射在设计模式和框架底层都会用到
2. 加载完类之后,在堆中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,形象的称之为:反射
二、示例
package com.hsp.baselearn.reflection.bean; public class User { public void doTest01(){ System.out.println("doTest01"); } public void doTest02(){ System.out.println("doTest02"); } }
package com.hsp.baselearn.reflection.test; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Test { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { String classPath="com.hsp.baselearn.reflection.bean.User"; String methodName="doTest02"; execute(classPath,methodName); } private static void execute(String classPath,String methodName) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { //(1) 加载类,返回Class类型的对象 Class aClass = Class.forName(classPath); Method method = aClass.getMethod(methodName); //(2) 通过cls 得到你加载的类 . Object o= aClass.newInstance();//创建对象 System.out.println("获取运行类型:"+o.getClass());//运行类型 //(3) 通过method1 调用方法:即通过方法对象来实现调用方法 method.invoke(o); } }
三、使用场景
JAVA反射机制可以完成
1.在运行时判断一个对象所属的类
2.在运行时构造任意一个类对象
3.在运行时得到任意一个类所具有的成员变量和方法
4.在运行时调用任意一个对象的成员变量和方法
5.生产动态代理
四、反射相关的主要类
1. java.lang.Class:代表一个类,Class对象表示某个类加载后在堆中的对象
2.java.lang.reflect.Method: 代表类的方法,Method对象表示某个类的方法
3.java.lang.reflect.Field:代表类的成员变量,Field对象表示某个类的成员变量
4.java.lang.reflect.Constructor:代表类的构造方法
package com.hsp.baselearn.reflection.test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Test { public static void main(String[] args) throws Exception { String classPath="com.hsp.baselearn.reflection.bean.User"; String methodName="doTest02"; execute(classPath,methodName); } private static void execute(String classPath,String methodName) throws Exception { //(1) 加载类,返回Class类型的对象 Class aClass = Class.forName(classPath); Method method = aClass.getMethod(methodName); //(2) 通过cls 得到你加载的类 . Object o= aClass.newInstance();//创建对象 System.out.println("获取运行类型:"+o.getClass());//运行类型 //(3) 通过method1 调用方法:即通过方法对象来实现调用方法 method.invoke(o);// 传统方法 对象.方法(),反射机制 方法.invoke(对象) //java.lang.reflect.Field: 代表类的成员变量,Field对象表示某个类的成员变量 //得到name字段 //getField不能得到私有的属性 // Field nameField= aClass.getField("name"); Field declaredField= aClass.getDeclaredField("name"); declaredField.setAccessible(true); System.out.println(declaredField.get(o));//传统写法 对象.成员变量,反射:成员变量对象.get(对象) //java.lang.reflect.Constructor: Constructor constructor = aClass.getConstructor();//()中可以指定构造器参数类型,返回无参构造其 System.out.println(constructor); Constructor constructorVal= aClass.getConstructor(String.class); //这里传入形参的class对象 System.out.println(constructorVal); } }
package com.hsp.baselearn.reflection.bean; public class User { public User(){} public User(String name){ this.name=name; } private String name="小明"; private String age="9"; public void doTest01(){ System.out.println("doTest01"); } public void doTest02(){ System.out.println("doTest02"); } }
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/15517417.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2020-11-10 Jwt学习笔记
2020-11-10 单点登录的三种方式
2020-11-10 用户登录业务介绍
2020-11-10 Redis