当前位置:网站首页>反射及在运用过程中出现的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中还可以对获取到的对象进一步操作。
边栏推荐
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
- 关于静态类型、动态类型、id、instancetype
- 多线程基础:线程基本概念与线程的创建
- 打家劫舍III[后序遍历与回溯+动态规划]
- RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
- R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
猜你喜欢
![[depth first search] Ji suanke: Square](/img/fc/e42ae0d036be258bed5623d55fc2db.jpg)
[depth first search] Ji suanke: Square

Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform

Implementation of AVL tree

Abstract classes and abstract methods
![Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting](/img/dd/c3f4a9c38b156e3a9b9adfd6253773.gif)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting

Helm deploy etcd cluster

Based on butterfly species recognition

Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!

Reptiles have a good time. Are you full? These three bottom lines must not be touched!

同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
随机推荐
Modulenotfounderror: no module named 'PIL' solution
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
[depth first search] Ji suanke: find numbers
JDBC详解
Excel 中VBA脚本的简单应用
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
数学知识——高斯消元(初等行变换解方程组)代码实现
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Oracle advanced (IV) table connection explanation
青龙面板最近的库
能源行业的数字化“新”运维
黑马--Redis篇
抽象类与抽象方法
pytorch常见损失函数
LeetCode-1279. Traffic light intersection
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人