当前位置:网站首页>反射及在运用过程中出现的IllegalAccessException异常
反射及在运用过程中出现的IllegalAccessException异常
2022-07-06 11:23:00 【qq_37705525】
首先了解一下反射:
反射:将类的各个组成部分封装成其他对象,这就是反射机制
在字节码文件加载进内存之后,会形成一个Class类对象
以下为获取Class对象的三种方式:
Class.forName(”全类名“):将字节码文件加载进内存,返回Class 对象
多用于配置文件,可以将类名定义在配置文件中。读取文件加载类
若字节码文件已经加载进内存,只需要通过类名.Class来获取
多用于参数的传递
已经存在实例化对象,可以通过对象.getClass()方法,getClass方法继承自Object类
多用于对象的获取字节码的方式
结论:同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,不论通过哪一种方法获取的Class对象都是同一个源代码中的成员方法、成员变量以及构造器会分别被封装为各自对应的数组对象
通过Class对象的获取方法可以分别获取到成员变量、成员方法和构造器:
因为成员方法、成员变量以及构造器的访问权限修饰符不同
不同的获取的获取方法可能会出现获取不到私有成员的情况,
相同的是,在获取到成员变量、成员方法和构造器后,都无法直接对私有权限修饰符修饰的成员变量和成员方法及构造器进行操作
如果直接对过去到的私有成员进行操作,会抛出IllegalAccessException异常
解决方案:忽略权限修饰符的安全检查
获取到的对象.setAccessible(true);
在获取到成员方法、成员变量、构造器之后,在相应的类method、Field、Constructor中还可以对获取到的对象进一步操作。
边栏推荐
- test about BinaryTree
- Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
- openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
- 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- tensorflow和torch代码验证cuda是否安装成功
- R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- [depth first search] Ji suanke: Square
- Human bone point detection: top-down (part of the theory)
猜你喜欢
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
How to improve website weight
On AAE
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
手写一个的在线聊天系统(原理篇1)
Interview assault 63: how to remove duplication in MySQL?
The second day of rhcsa study
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
About NPM install error 1
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
随机推荐
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
Interview assault 63: how to remove duplication in MySQL?
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
Deep circulation network long-term blood pressure prediction [translation]
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Characteristic colleges and universities, jointly build Netease Industrial College
test about BinaryTree
LeetCode-1279. 红绿灯路口
The nearest library of Qinglong panel
手写一个的在线聊天系统(原理篇1)
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
ACTF 2022圆满落幕,0ops战队二连冠!!
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries