当前位置:网站首页>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;
}
}
边栏推荐
- Variable star --- article module (1)
- Why do novices often fail to answer questions in the programming community, and even get ridiculed?
- Reference frame generation based on deep learning
- 华为设备命令
- [weekly pit] calculate the sum of primes within 100 + [answer] output triangle
- Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
- 动态切换数据源
- OLED屏幕的使用
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- Spark SQL chasing Wife Series (initial understanding)
猜你喜欢
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Pycharm remote execution
PHP saves session data to MySQL database
[MySQL] basic use of cursor
【mysql】触发器
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Performance test process and plan
随机推荐
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
性能测试过程和计划
基于STM32单片机设计的红外测温仪(带人脸检测)
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
@PathVariable
Design your security architecture OKR
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
Spark SQL chasing Wife Series (initial understanding)
[diy] how to make a personalized radio
Entity alignment two of knowledge map
It's almost the new year, and my heart is lazy
Performance test process and plan
[DIY]如何制作一款个性的收音机
如何实现常见框架
PHP saves session data to MySQL database
【OpenCV 例程200篇】220.对图像进行马赛克处理
Logic is a good thing
Pat 1085 perfect sequence (25 points) perfect sequence
Dynamically switch data sources
爱可可AI前沿推介(7.6)