当前位置:网站首页>Reflection - class object function - get method (case)
Reflection - class object function - get method (case)
2022-06-24 20:58:00 【Platonic】
Get the object's 3 Ways of planting :
1.Class.forNane(" Full class name "): Load the bytecode file into memory , return Class object
apply : More for configuration files , Define the class name in the configuration file . Read the file , Load class
2. Class name .class: Through the properties of the class name class obtain
apply : It is mainly used for parameter passing
3. object .getClass():getClass() Method in Object It defines
apply : It is mainly used for obtaining bytecode of objects
summary : The same bytecode file (*.class) In a program run , It will only be loaded once , Either way class The objects are all the same .
Case study :
* demand : Write a framework , You can't change any code of this class , Can help us create objects of any class , And execute any of these methods
* Realization :
1. The configuration file
2. Reflection
* step :
1. Define the full class name of the object to be created and the method to be executed in the configuration file
2. Load the read configuration file in the program
3. Use reflection technology to load class files into memory
4. Create objects
5. Execution method
Example :
First create three classes :student.java ;Person.java And a main method ReflectTest class
They are as follows :
public class Student {
public void sleep(){
System.out.println("sleep...");
}
}
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void eat(){
System.out.println("eat...");
}
}
public class ReflectTest {
public static void main(String[] args) throws Exception {
// You can create objects of any kind , Any method can be executed
/*
* Premise : You can't change any code for this class , You can create objects of any kind , Any method can be executed
* */
/* Person p=new Person();
p.eat();*/
//1. Load the file
//1.1 establish Properties object
Properties p=new Properties();
//1.2 Load profile , Convert to a collection
//1.2.1 obtain class The configuration file under the directory
ClassLoader cls = ReflectTest.class.getClassLoader();
InputStream is = cls.getResourceAsStream("pro.properties");
p.load(is);
//2. Get the data defined in the configuration file
String className = p.getProperty("className");
String methodName = p.getProperty("methodName");
//3. Load this class into memory
Class aClass = Class.forName(className);
//4. Create objects
Object obj = aClass.newInstance();
//5. Get object method
Method method = aClass.getMethod(methodName);
//6. Execution method
method.invoke(obj);
}
}The second step is src in new File Name a new file :pro.proterties
className=cn.itcast.junit.domain.Student
methodName=sleepThe third step : function ReflectTest class , give the result as follows
sleep...
边栏推荐
- With its own cells as raw materials, the first 3D printing ear transplantation was successful! More complex organs can be printed in the future
- 用手机摄像头就能捕捉指纹?!准确度堪比签字画押,专家:你们在加剧歧视
- Prototype mode -- clone monster Army
- Bytebase joins Alibaba cloud polardb open source database community
- Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
- Leetcode(135)——分发糖果
- The Google File System (GFS) learning notes
- Jd.com: how does redis implement inventory deduction? How to prevent oversold?
- A/B测试助力游戏业务增长
- What are the problems with traditional IO? Why is zero copy introduced?
猜你喜欢

红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定

How Fiddler works

Apple doesn't need money, but it has no confidence in its content

The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022

I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world

Basic properties and ergodicity of binary tree

"Super point" in "Meng Hua Lu", is the goose wronged?

Enjoy yuan mode -- a large number of flying dragons

云计算发展的 4 个阶段,终于有人讲明白了

The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time
随机推荐
With its own cells as raw materials, the first 3D printing ear transplantation was successful! More complex organs can be printed in the future
Use the transparent [x] cross button image in the dialog
Bytebase加入阿里云PolarDB开源数据库社区
DAPP system customization of full chain hash game (scheme design)
宅男救不了元宇宙
Visitor model -- generation gap between young and middle-aged people
海泰前沿技术|隐私计算技术在医疗数据保护中的应用
得物多活架构设计之路由服务设计
对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿
建立自己的网站(14)
Leetcode (455) - distribute cookies
You can capture fingerprints with a mobile camera?! Accuracy comparable to signature and monogram, expert: you are aggravating discrimination
How Fiddler works
传统的IO存在什么问题?为什么引入零拷贝的?
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
Openvino2022 dev tools installation and use
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
Apple doesn't need money, but it has no confidence in its content