当前位置:网站首页>Reflection operation exercise
Reflection operation exercise
2022-07-06 21:01:00 【zhang__ one thousand two hundred and thirty-four】
Create a new file through reflection
package com.zqf.fanshe.Class_;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class reflection08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> fileClass = Class.forName("java.io.File");
Constructor<?> declaredConstructor1 = fileClass.getDeclaredConstructor(String.class);
String filepath="C:\\idea\\xiangmu\\newnew.txt";
Object file = declaredConstructor1.newInstance(filepath);// establish File object
// Get method object
Method createNewFile = fileClass.getMethod("createNewFile");
createNewFile.invoke(file);
System.out.println(filepath.getClass());
System.out.println(" File created successfully "+filepath);
}
}
Modify the value of private attribute through reflection And call public methods
package com.zqf.fanshe.Class_;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class reflection07 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
// obtain Test Object's Class object
Class<?> testClass = Class.forName("com.zqf.fanshe.Class_.Test");
// Create an object instance
Object o = testClass.newInstance();
// Get the attribute value
Field field = testClass.getDeclaredField("name");
field.setAccessible(true);
field.set(o,"hello jack");
// Get public method getname
Method method = testClass.getMethod("getName");
Object invoke = method.invoke(o);
System.out.println("name The value of the property "+invoke);
}
}
class Test{
private String name="hellokitty";
public String getName() {
return name;
}
}
边栏推荐
- User defined current limiting annotation
- [DSP] [Part 2] understand c6678 and create project
- [DSP] [Part 1] start DSP learning
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- OSPF多区域配置
- @PathVariable
- OAI 5g nr+usrp b210 installation and construction
- [DIY]自己设计微软MakeCode街机,官方开源软硬件
- Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
- 使用.Net驱动Jetson Nano的OLED显示屏
猜你喜欢

C language operators

Swagger UI教程 API 文档神器

Entity alignment two of knowledge map

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

基于STM32单片机设计的红外测温仪(带人脸检测)

Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

PHP online examination system version 4.0 source code computer + mobile terminal

拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条

SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
随机推荐
[DSP] [Part 2] understand c6678 and create project
2110 summary of knowledge points and common problems in redis class
(工作记录)2020年3月11日至2021年3月15日
How to implement common frameworks
Redis insert data garbled solution
使用.Net分析.Net达人挑战赛参与情况
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
(work record) March 11, 2020 to March 15, 2021
7. Data permission annotation
Taylor series fast Fourier transform (FFT)
PHP online examination system version 4.0 source code computer + mobile terminal
Pat 1085 perfect sequence (25 points) perfect sequence
Pytest (3) - Test naming rules
PHP saves session data to MySQL database
Common English vocabulary that every programmer must master (recommended Collection)
2022 nurse (primary) examination questions and new nurse (primary) examination questions
正则表达式收集
It's almost the new year, and my heart is lazy
Comment faire une radio personnalisée
快过年了,心也懒了