当前位置:网站首页>Get class files and attributes by reflection
Get class files and attributes by reflection
2022-07-05 06:36:00 【Said it was all】
The following code is to be obtained by using the reflection mechanism :
package com.zzuli;
/** * @author luoqingchen * @reate 2022-02-03-9:46 */
public class reflect01 {
private boolean flag;
public int a;
protected String s;
}
Reflection acquisition :
public class Test01 {
public static void main(String[] args) throws Exception {
Class reflect=Class.forName("com.zzuli.reflect01");
StringBuilder s=new StringBuilder();
s.append(Modifier.toString(reflect.getModifiers())+" class "+reflect.getSimpleName());
s.append("\n");
s.append("{");
s.append("\n");
Field[] fields= reflect.getDeclaredFields();
for (Field fs:fields){
s.append("\t\t");
s.append( Modifier.toString(fs.getModifiers()));
s.append(" ");
s.append(fs.getType().getSimpleName());
s.append(" ");
s.append(fs.getName());
s.append("\n");
}
s.append("\n");
s.append("}");
System.out.println(s);
}
}
Running results :
Use reflection to get the attribute value in the class :
public class Test02 {
public static void main(String[] args) throws Exception{
Class reflect=Class.forName("com.zzuli.reflect01");
Object obj=reflect.newInstance();
// get attribute
Field field=reflect.getDeclaredField("a");
// Setting property values
field.set(obj,8);
Object obj2=field.get(obj);
// Get the attribute value
System.out.println(obj2);
}
}
Running results :( This example is getting public The value of the property )
Want to get the value of the private attribute , Need to break the package :
public class Test02 {
public static void main(String[] args) throws Exception{
Class reflect=Class.forName("com.zzuli.reflect01");
Object obj=reflect.newInstance();
// get attribute
Field field=reflect.getDeclaredField("flag");
// Break the package
field.setAccessible(true);
// Setting property values
field.set(obj,true);
Object obj2=field.get(obj);
// Get the attribute value
System.out.println(obj2);
}
}
Running results :
边栏推荐
- Redis-01.初识Redis
- Game theory acwing 892 Steps Nim game
- Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
- C - XOR to all (binary topic)
- Vant weapp swippecell set multiple buttons
- Modnet matting model reproduction
- AE tutorial - path growth animation
- Design specification for mobile folding screen
- What's wrong with this paragraph that doesn't work? (unresolved)
- TCP's understanding of three handshakes and four waves
猜你喜欢
4. Oracle redo log file management
The “mode“ argument must be integer. Received an instance of Object
容斥原理 AcWing 890. 能被整除的数
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
Find the combination number acwing 887 Find combination number III
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
[algorithm post interview] interview questions of a small factory
Vscode configures the typera editor for MD
1.手动创建Oracle数据库
博弈论 AcWing 892. 台阶-Nim游戏
随机推荐
Vant weapp swippecell set multiple buttons
达梦数据库全部
[Chongqing Guangdong education] National Open University 2018 autumn 0702-22t contemporary Chinese political system reference questions
【LeetCode】Easy | 20. Valid parentheses
Design specification for mobile folding screen
代码中的英语全部
Find the combination number acwing 888 Find the combination number IV
【高德地图POI踩坑】AMap.PlaceSearch无法使用
How to understand the definition of sequence limit?
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Find the combination number acwing 889 01 sequence meeting conditions
4. Oracle redo log file management
AE tutorial - path growth animation
H5 module suspension drag effect
1.手动创建Oracle数据库
数据库Mysql全部
5. Oracle tablespace
The “mode“ argument must be integer. Received an instance of Object
our solution
求组合数 AcWing 888. 求组合数 IV