当前位置:网站首页>Brief introduction of reflection mechanism
Brief introduction of reflection mechanism
2022-07-26 13:22:00 【Suzerk】
Reflection is Java Seen as the key to dynamic language , The reflection mechanism allows programs to use reflection during execution API Get internal information about any class , And can directly operate the internal properties and methods of any object .
obtain Class Common methods of objects :
Class c=Class.forName("java.lang.String");
Even if it's private Modified keywords can also be read .
function :
1) Judge the member variables and methods of any class at run time ;
2) Get generic information at runtime ;
3) Processing annotations at run time ;
4) Generate dynamic agents ;
5) At run time, determine the class that any object belongs to ;
6) Construct an object of any class at run time ;
instanceofUsed to determine whether it is an instance of a class :
System.out.println(Class.forName(properties.getProperty("FactoryName")).newInstance() instanceof IFactory );
- Use
newInstance()Method to createClassObject corresponds to an instance of the class .
IFactory operFactory = (IFactory) Class.forName("AddFactory").newInstance();
- Advantages and disadvantages :
advantage :
1) It improves the flexibility and expansibility of the program , Reduce module coupling ;
2) Get the various contents of the class at run time , Method of calling object .
3) Using reflection prevents code from being written dead in the framework .
shortcoming :
1) Reflection consumes a certain amount of system resources , The efficiency of reflection operation is much lower than that of normal operation ;
2) The permission check can be ignored when the reflection calls the method , As a result, encapsulation may be broken and security problems may arise .
边栏推荐
- JUC总结
- 【开源之美】nanomsg(2) :req/rep 模式
- 华为机考 ~ 偏移量实现字符串加密
- 1312_ Apply 7z command for compression and decompression
- 1312_适用7z命令进行压缩与解压
- 多线程使用不当导致的 OOM
- Leetcode 217. there are duplicate elements
- MySQL data directory (3) -- table data structure MyISAM (XXVI)
- Outline design specification
- Mysql数据目录(3)---表数据结构myISAM(二十六)
猜你喜欢

目标检测网络R-CNN 系列

如何构建以客户为中心的产品蓝图:来自首席技术官的建议

1312_ Apply 7z command for compression and decompression
![[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp](/img/d4/7b9c7c99d46661e1be2963a342dd18.jpg)
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp

Learn about Pinia state getters actions plugins

Flutter multi-channel packaging operation

AI-理论-知识图谱1-基础

Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web

Elementary notes of binary tree

Slam 02. overall framework
随机推荐
[typescript] typescript common types (Part 2)
Use grid to realize left, middle and right layout, and the middle content is adaptive
AI theory knowledge map 1 Foundation
Use float to realize left, middle and right layout, and the middle content is adaptive
B+树挑选索引(1)---mysql从入门到精通(二十二)
B+ tree selection index (2) -- MySQL from entry to proficiency (23)
Can MySQL customize variable parameter storage functions?
Incorrect use of parentdatawidget when the exception was thrown, this was the stack:
时间复杂度和空间复杂度
《Kotlin系列》之MVVM架构封装(kotlin+mvvm)
[5g] what are Cu and Du in 5g?
This article explains the FS file module and path module in nodejs in detail
B+树索引使用(8)排序使用及其注意事项(二十)
华为机考 ~ 偏移量实现字符串加密
LeetCode 217. 存在重复元素
B+树索引使用(9)分组、回表、覆盖索引(二十一)
Reflection, an implementation of automatic repeated call interface
[flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
父组件访问子组件的方法或参数 (子组件暴漏出方法defineExpose)
Use flex to realize left middle right layout and middle adaptation