当前位置:网站首页>Operate attribute chestnut through reflection
Operate attribute chestnut through reflection
2022-06-23 02:08:00 【Doghead Intern】
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class student = Student.class;
// Get a parameterless construction object through reflection
Student s1 = (Student) student.newInstance();
System.out.println(s1);
// Get a parameterized construction object through reflection
Constructor constructor = student.getConstructor(Integer.class, String.class, int.class);
Student s2 = (Student) constructor.newInstance(18, " Xiaohong ", 1);
System.out.println(s2);
// Call normal methods through reflection
// Get an object first
Student s3 = (Student) student.newInstance();
// Get a way
Method setName = student.getDeclaredMethod("setName", String.class);
//invoke: Activate ( object , The value of the method )
setName.invoke(s3," Red red ");
System.out.println(s3.getName());
// Manipulate attributes by reflection
Student s4 = (Student) student.newInstance();
Field name = student.getDeclaredField("name");
/// Private properties cannot be manipulated directly
// Turn off security detection
name.setAccessible(true);
name.set(s4," Sensitive ");
System.out.println(s4.getName());
}边栏推荐
- Common mistakes in C language (sizeof and strlen)
- 5g core network and core network evolution
- 1.3-1.4 web page data capture
- 165. cat climbing
- Hello
- On AI and its future trend | community essay solicitation
- 2022-1-12
- Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
- Why is BeanUtils not recommended?
- JS rotation chart (Netease cloud rotation chart)
猜你喜欢

3. compilation and linking principle

There are animation characters interacting with each other when the mouse slides in the web page

For Xiaobai who just learned to crawl, you can understand it after reading it

Byte order: big endian vs little endian

1. introduction to MySQL database connection pool function technology points

Reptile lesson 1

Campus network AC authentication failed

Rebirth -- C language and the story I have to tell (text)

Evolution history of mobile communication

Li Mu's notes on machine learning -1.2
随机推荐
Summary of the first week of winter vacation
Targeted and ready to go
"First knowledge of C language" (Part 3)
Express framework installation and start service
Performance test -- 14 detailed explanation of performance test report and precautions
Nebula operator cloud practice
Branch and loop statements (including goto statements) -part1
Cmake passing related macros to source code
JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
Bc117 xiaolele walks up the steps
Single chip microcomputer (STC series 8051 core single chip microcomputer)
2D prefix and
Third order magic cube formula
Common mistakes in C language (sizeof and strlen)
On AI and its future trend | community essay solicitation
Special exercise split line-----------------------------
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
Anaconda creates a new environment encounter pit
fatal: refusing to merge unrelated histories
3. compilation and linking principle