網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
文章目錄
一、更改string的value值,但是其指向不變
曾經(jīng)看到過(guò)這樣的一個(gè)題目:
public static void main(String[] args) {
String s=new String(“abc”);
//在這中間添加N行代碼,但是必須保證s引用的指向不變,最終將輸出變成abcd
System.out.println(s);
}
看到如上題目,該如何加代碼,能讓其指向不變呢?解決辦法如下:
String s = new String(“abc”);
Field value = s.getClass().getDeclaredField(“value”);
value.setAccessible(true);
value.set(s, “abcd”.toCharArray());
System.out.println(s);
采用了反射機(jī)制實(shí)現(xiàn)了其功能實(shí)現(xiàn),我們發(fā)現(xiàn)代碼中有一個(gè)setAccessible(true);如果去掉這句話會(huì)如何呢?
Exception in thread "main" java.lang.IllegalAccessException: Class FileTest can not access a member of class java.lang.String with modifiers "private final"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
at java.lang.reflect.Field.set(Field.java:761)
at FileTest.main(FileTest.java:17)
Class FileTest can not access a member of class java.lang.String with modifiers "private final"意思就是類 FileTest 不能訪問(wèn)帶有修飾符“private final”的類 java.lang.String 的成員。
二、實(shí)體demo 獲取成員變量的值
1)私有成員變量
在平常創(chuàng)建正常model使用過(guò)程中,都是設(shè)置private類型的成員變量
public class Student {
private String name;
private Integer age;
Student student=new Student();
student.setName("huo");
Field value1=Student.class.getDeclaredField("name");
// value1.setAccessible(true);
System.out.println(value1.isAccessible());
System.out.println(value1.get(student));
執(zhí)行如上代碼,會(huì)輸出什么?
false
Exception in thread "main" java.lang.IllegalAccessException: Class FileTest can not access a member of class Student with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
at java.lang.reflect.Field.get(Field.java:390)
at FileTest.main(FileTest.java:27)
其實(shí)這種情況和上述對(duì)string的demo是一樣的。 那么我們能如何獲取值呢?
兩種辦法:
- 將私有變量改為公有變量
- 配置 value1.setAccessible(true);
2)公有成員變量
public class Student {
public String name;
public Integer age;
看執(zhí)行結(jié)果:
false
huo
3)setAccessible
Student student=new Student();
student.setName("huo");
Field value1=Student.class.getDeclaredField("name");
value1.setAccessible(true);
System.out.println(value1.isAccessible());
System.out.println(value1.get(student));
看執(zhí)行結(jié)果:
true
huo
4)成員變量為public,isAccessible會(huì)輸出什么?
可以測(cè)試一下,答案為true,為什么呢?
三、setAccessible的作用到底是什么?
看如上解釋,清晰明了。為TRUE表示反射在使用時(shí)應(yīng)禁止java語(yǔ)言訪問(wèn)檢查,值為false表示反射對(duì)象應(yīng)強(qiáng)制執(zhí)行java語(yǔ)言訪問(wèn)檢查。
這樣就能很明了設(shè)置了setAccessible為true之后,就能編輯final變量以及訪問(wèn)private變量了。
下篇講述java API之AccessibleObject,也就是Field的父類到底干了點(diǎn)什么?
原文鏈接:https://blog.csdn.net/huo065000/article/details/119919185
相關(guān)推薦
- 2021-12-15 CentOS下更新SQLite版本_SQLite
- 2022-01-15 Meteor 項(xiàng)目部署至服務(wù)器(windows)
- 2022-10-08 C++淺析函數(shù)重載是什么_C 語(yǔ)言
- 2022-10-17 Qt線程池QThreadPool的使用詳解_C 語(yǔ)言
- 2024-04-23 Win11老是提示資源管理器已停止工作怎么解決
- 2022-09-09 Nginx配置解決NetCore的跨域問(wèn)題_nginx
- 2022-11-04 深入了解Python中Lambda函數(shù)的用法_python
- 2022-09-26 Python?Celery定時(shí)任務(wù)詳細(xì)講解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支