当前位置:网站首页>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;
}
}
边栏推荐
- 使用.Net分析.Net达人挑战赛参与情况
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- The biggest pain point of traffic management - the resource utilization rate cannot go up
- Design your security architecture OKR
- Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- C language games - minesweeping
- OAI 5G NR+USRP B210安装搭建
- 使用.Net驱动Jetson Nano的OLED显示屏
- 7、数据权限注解
猜你喜欢

强化学习-学习笔记5 | AlphaGo

Comment faire une radio personnalisée

Distributed ID

Why do novices often fail to answer questions in the programming community, and even get ridiculed?

Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)

New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue

【微信小程序】运行机制和更新机制
![[DSP] [Part 1] start DSP learning](/img/81/051059958dfb050cb04b8116d3d2a8.png)
[DSP] [Part 1] start DSP learning

Data Lake (VIII): Iceberg data storage format

【DSP】【第一篇】开始DSP学习
随机推荐
[DIY]如何制作一款个性的收音机
What are RDB and AOF
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
3D人脸重建:从基础知识到识别/重建方法!
[DIY]自己设计微软MakeCode街机,官方开源软硬件
SSO single sign on
Interviewer: what is the internal implementation of ordered collection in redis?
Recyclerview GridLayout bisects the middle blank area
How to implement common frameworks
知识图谱之实体对齐二
How to turn a multi digit number into a digital list
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
【微信小程序】运行机制和更新机制
C # use Oracle stored procedure to obtain result set instance
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
使用.Net驱动Jetson Nano的OLED显示屏
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
It's almost the new year, and my heart is lazy
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)