当前位置:网站首页>反射
反射
2022-06-29 16:58:00 【华为云】
反射机制1、反射机制有什么用?通过java语言中的反射机制可以操作字节码文件。可以读和修改字节码文件。通过反射机制可以操作代码片段。(class文件)
2、反射机制的相关类在哪个包下?java.lang.reflect.*;
3、反射机制相关的重要的类有哪些?java.lang.Class:代表整个字节码,代表一个类型。
java.lang.reflect.Method:代表字节码中的方法字节码。java.lang.reflect.Constructor:代表字节码中的构造方法字节码。java.lang.reflect.Field:代表字节码中的属性字节码。4、要操作一个类的字节码,首先需要获取到这个类的字节码,怎么获取java.lang.Class实例?三种方式:第一种:Class c = Class.forName("完整类名带包名");
第二种:Class c = 对象.getClass(); 第三种:Class c = 任何类型.class; Class s = String.class; // s代表String类型 Class d = Date.class; //k代表Date类型 Class i = int.class; //i代表int类型5、用反射机制创建Java对象Class c = Class.forName("完整类名");Object obj = c.newInstance();
还可以结合配置文件使用,forName()括号里面可以填写key,通过key来获取value,此时这个key就相当于完整的类名。(如果要一次创建多个对象,可以创建数组)优点:java代码每写一遍,在不改变java源代码的基础上,可以做到不同对象的实例化。 符合OCP开闭原则:对扩展开放,对修改关闭。(程序更加的灵活)6、Class.forName()这个方法会导致:类加载。 而类加载时静态代码块就会执行。所以,如果只想执行静态代码块中的代码,可以直接写成:Class.forName("完整类名");
7、以下代码可以拿到一个文件的绝对路径String path = Thread.currentThread().getContextClassLoader().getResource("文件名").getPath();
代码解释: Thread.currentThread(); 当前线程对象 getContextClassLoader(); 是线程对象的方法,可以获取到当前线程的类加载器对象 getResource(); 【加载资源】这是类加载器对象的方法,当前线程的类加载器默认从类的根路径下加载资源 getPath(); 获取路径注意:文件必须要在类文件下面,即src下面。8、直接以流的形式返回InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("文件名");
9、java.util包下提供了一个资源绑定器,便于获取属性配置文件中的内容。 使用以下这种方式的时候,属性配置文件xxx.properties必须放到类路径下。
//资源绑定器,只能绑定xxx.properties文件。并且这个文件必须放到类路径下。文件扩展名也必须是properties//并且在写路径的时候,路径后面的扩展名不能写。ResourceBundle bundle = ResourceBundle.getBundle("文件名");String className = bundle.getString(key:" ");System.out.pritnln(classNmae);(以后就使用以上方法就行。)10、关于JDK中自带的类加载器:10.1、什么是类加载器?专门负责加载类的命令/工具。ClassLoader
10.2、JDK中自带了3个类加载器 启动类加载器:rt.jar 扩展类加载器:ext/*.jar 应用类加载器:classpath --->应用类即自己写的类10.3、假设有这样一段代码: String s = "abc"; 代码在开始执行之前,会将所需要的类全部加载到JVM当中。 通过类加载器加载,看到以上代码,类加载器会找string.class 文件,找到就加载,那是怎么加载的呢? 首先通过“启动类加载器”加载。 注意:启动类加载器专门加载:rt.jar文件 rt.jar中都是JDK最核心的类库。 如果通过“启动类加载器”加载不到的时候, 会通过“扩展类加载器”加载。 注意:扩展类加载器专门加载ext/*.jar 如果“扩展类加载器”没有加载到,那么 会通过“应用类加载器”加载。 注意:应用类加载器专门加载:classpath中的类。10.4、java中为了保证类加载的安全,使用了双亲委派机制。 优先从启动类加载中加载,这个称为“父”, “父”无法加载到,再从扩展类加载,这个称为“母”。 双亲委派,如果都加载不到,才会考虑从应用类加载器中 加载。直到加载到位置。边栏推荐
- 基于C语言开发实现的一个用户级线程库
- 使用kalibr标定工具进行单目相机和双目相机的标定
- SLAM中的子图
- 6.25AtCoderABC257E - Addition and Multiplication 2题解
- Calibration of monocular camera and binocular camera with kalibr calibration tool
- controller、service、dao之间的关系
- XAMPP Apache安装时问题总结
- Why does selenium become the first choice for web automated testing? (source code attached)
- Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
- C language microblog user management system
猜你喜欢

@Component与@Configuration区别

curl: (56) Recv failure: Connection reset by peer

Greedy Apple plans to raise the price of iphone14, which will provide opportunities for Chinese mobile phones

最高81.98%!超百所“双一流”高校本科深造率公布

A tour of grpc:02 - generate code from proto

Étalonnage de la caméra monoculaire et de la caméra binoculaire à l'aide de l'outil d'étalonnage kalibr

Paper notes: e (n) equivariant graph neural networks

STM32 key chattering elimination -- Thinking of entry state machine

Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool

Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
随机推荐
Gradle下载慢或无法下载
解题元宇宙,网络游戏中的多元通信方案
6.26CF模拟赛D:黑白条题题解
Stable currency risk profile: are usdt and usdc safe?
A tour of gRPC:02 - 从proto生成代码
深度剖析monai(一) Data和Transforms部分
Which parameter is the partition information adjusted? The MySQL source stream API is used, not the table API
腾讯云发布CDW ClickHouse升级版,为海量数据实时分析场景提供极速体验
Summary of problems during xampp Apache installation
C# Winfrom Chart图表控件 柱状图、折线图
Advanced MySQL - storage engine
How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock
SpingMVC请求和响应
mysql数据库扫盲,你真的知道什么是数据库嘛
C winfrom chart chart control bar chart and line chart
力扣解法汇总535-TinyURL 的加密与解密
New feature of C11 - Auto and decltype type type indicators
Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
如何配置 logback?30分钟让你彻底学会代码熬夜敲
函数计算异步任务能力介绍 - 任务触发去重