当前位置:网站首页>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;
}
}
边栏推荐
- Swagger UI教程 API 文档神器
- Pytest (3) - Test naming rules
- #yyds干货盘点#重新梳理箭头函数的this
- 基于深度学习的参考帧生成
- The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
- 【DSP】【第一篇】开始DSP学习
- 知识图谱之实体对齐二
- [DIY]如何制作一款个性的收音机
- R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
- 2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
猜你喜欢

Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit

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

Design your security architecture OKR

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

【OpenCV 例程200篇】220.对图像进行马赛克处理

Manifest of SAP ui5 framework json

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

2022 portal crane driver registration examination and portal crane driver examination materials

2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest

C language operators
随机推荐
2022 nurse (primary) examination questions and new nurse (primary) examination questions
(work record) March 11, 2020 to March 15, 2021
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
Manifest of SAP ui5 framework json
Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
Common doubts about the introduction of APS by enterprises
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
【微信小程序】运行机制和更新机制
PHP saves session data to MySQL database
基于深度学习的参考帧生成
[DSP] [Part 2] understand c6678 and create project
[diy] self designed Microsoft makecode arcade, official open source software and hardware
Pytest (3) - Test naming rules
强化学习-学习笔记5 | AlphaGo
防火墙基础之外网服务器区部署和双机热备
2110 summary of knowledge points and common problems in redis class
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
使用.Net分析.Net达人挑战赛参与情况
Implementation of packaging video into MP4 format and storing it in TF Card