当前位置:网站首页>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 :
边栏推荐
- Day 2 document
- 博弈论 AcWing 894. 拆分-Nim游戏
- 11-gorm-v2-03-basic query
- Dataframe (1): introduction and creation of dataframe
- H5 embedded app adapts to dark mode
- Single chip computer engineering experience - layered idea
- TCP's understanding of three handshakes and four waves
- how to understand the “model independent.“
- vsCode创建自己的代码模板
- Financial risk control practice -- feature derivation based on time series
猜你喜欢

Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter

Knapsack problem acwing 9 Group knapsack problem

7. Oracle table structure

Genesis builds a new generation of credit system

Financial risk control practice -- feature derivation based on time series

Redis-01.初识Redis

Operator priority, one catch, no doubt

vsCode创建自己的代码模板

SolidWorks template and design library are convenient for designers to call

Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
随机推荐
Redis-01.初识Redis
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
SolidWorks template and design library are convenient for designers to call
Redis-01. First meet redis
P3265 [jloi2015] equipment purchase
June 29, 2022 daily
5. Oracle tablespace
Utf8 encoding
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
博弈论 AcWing 891. Nim游戏
2022/6/29-日报
2022-5-第四周日报
Huawei bracelet, how to add medicine reminder?
H5 module suspension drag effect
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Chinese remainder theorem acwing 204 Strange way of expressing integers
MySQL (UDF authorization)
Record the process of configuring nccl and horovod in these two days (original)