当前位置:网站首页>反射及在运用过程中出现的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中还可以对获取到的对象进一步操作。
边栏推荐
- 青龙面板最近的库
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
- Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
- R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
- Handwritten online chat system (principle part 1)
- Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- 如何提高网站权重
- How to improve website weight
猜你喜欢
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
wx小程序学习笔记day01
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
Druid 数据库连接池 详解
About NPM install error 1
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
A method of removing text blur based on pixel repair
Pytorch common loss function
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
随机推荐
Human bone point detection: top-down (part of the theory)
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
上海部分招工市場對新冠陽性康複者拒絕招錄
Test technology stack arrangement -- self cultivation of test development engineers
A method of removing text blur based on pixel repair
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
青龙面板最近的库
Leetcode topic [array] - 119 Yang Hui triangle II
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
Php+redis realizes the function of canceling orders over time
Synchronous development of business and application: strategic suggestions for application modernization
抽象类与抽象方法
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
MATLAB中deg2rad和rad2deg函数的使用
php+redis实现超时取消订单功能
AcWing 3537.树查找 完全二叉树
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
Interview assault 63: how to remove duplication in MySQL?