<> reflex

<> definition

Popular speaking , Encapsulates the components of a class into other objects , This is the reflection mechanism

<> effect
1. During the running of the program , Manipulate these objects . 2. Can be decoupled , Improve the scalability of programs .
<> principle

Let's get to know it first java Several stages of code running in a computer :

Corresponding to the above three stages , obtain Class There are three ways to do this :
1. Class.forName(" Full class name "): Load bytecode file into memory , return Class object * More for configuration files , Define the class name in the configuration file . read file , Loading classes 2.
Class name .class: Attribute by class name class obtain * It is mostly used for parameter transmission 3. object .getClass():getClass() Methods in the Object Class . *
The way to get bytecode for objects
Same bytecode file (*.class) During a program run , It will only be loaded once , Either way Class Objects are the same .

<>Class Object function
1. Get the member variables * Field[] getFields() : Get all public Decorated member variable * Field getField(String
name) Gets the public Decorated member variable * Field[] getDeclaredFields() Get all member variables , Don't consider modifiers *
FieldgetDeclaredField(String name) 2. Get constructors * Constructor<?>[] getConstructors
() * Constructor<T> getConstructor( class <?>... parameterTypes) * Constructor<T>
getDeclaredConstructor( class <?>... parameterTypes) * Constructor<?>[]
getDeclaredConstructors() 3. How to get members : * Method[] getMethods() * Method getMethod
(String name, class <?>... parameterTypes) * Method[] getDeclaredMethods() * Method
getDeclaredMethod(String name, class <?>... parameterTypes) 4. Get full class name * String getName
<>Field class
* Operation member variable : 1. Setting value * void set(Object obj, Object value) 2. Get value * get(Object obj)
3. Ignore security checks for access modifiers * setAccessible(true): Violent reflex
<>Constructor class
* create object ( Construction method ): * T newInstance(Object... initargs) *
If you use the null parameter constructor to create an object , The operation can be simplified :Class Object's newInstance method
<>Method class
* Implementation method : * Object invoke(Object obj, Object... args) * Get method name : * String getName:
Get method name
<> Examples

Using reflection , Write one " frame ", Can not change any code of the class , It can help us create objects of any class , And execute any of them

Write a student class
package cn.domain; public class Student { public void sleep(){ System.out.
println("sleep..."); } }
Write configuration file pro.properties
className=cn.domain.Student methodName=sleep
Write framework class
package cn.reflect; import cn.domain.Person; import cn.domain.Student; import
java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method
; import java.util.Properties; /** * Frame class */ public class ReflectTest { public
static void main(String[] args) throws Exception { // You can create objects of any class , Any method can be executed
//1. Load configuration file //1.1 establish Properties object Properties pro = new Properties();
//1.2 Load configuration file , Convert to a collection //1.2.1 obtain class The configuration file in the directory ClassLoader classLoader = ReflectTest
.class.getClassLoader(); InputStream is = classLoader.getResourceAsStream(
"pro.properties"); pro.load(is); //2. Gets the data defined in the configuration file String className = pro.
getProperty("className"); String methodName = pro.getProperty("methodName");
//3. Load the class into memory Class cls = Class.forName(className); //4. create object Object obj = cls.
newInstance(); //5. Get method object Method method = cls.getMethod(methodName); //6. Implementation method
method.invoke(obj); } }
In this way, a simple implementation is achieved through the reflection mechanism ’ frame ’ The compilation of

©2019-2020 Toolsou All rights reserved,
python3 Of tkinter Design of login interface +mysql Import data of database CRC Partial understanding of modular division use MySQL Processing 100 million level data The greatest common divisor and the least common multiple C Language Edition 【 Data structure and algorithm 8】 The maze problem of recursion 2020 year ,Java Is there any future ?【2020 The best little program : You can find all kinds of resources you want ?】element Form validation number type and range An interview of Alibaba outsourcing vue:word Local upload Preview