Basic use of java reflection

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[basic use of java reflection]

Background introduction

Java reflection mechanism is to know all the properties and methods of any class in the running state;

Any method of any object can be called;

This kind of dynamically acquired information and the function of dynamically calling object methods are called the reflection mechanism of Java language

Knowledge analysis

Class loading

Class loading refers to reading the class file of a class into memory and creating a Java Lang. class object.

In other words, when a program uses any class, the system will create a Java The loading of lang.class object classes is completed by the class loader, which is usually provided by the JVM

Class object

Class is the abstraction of a class of objects. Similarly, class is the abstraction of a class. The essence of a class is actually an object. Class objects are used to store the basic information of a class: class size, class name, class version, inheritance level, and the mapping table between messages and functions

Coding practice

Get class object: you can use

 Class. Forname, class Class, object getClass()

To get the class object of the class

Create object:

1. Use the newinstance () method of class object to create an instance of the corresponding class 2 Use the class object to obtain the constructor object, and then call the newinstance method of the constructor object to create the corresponding instance

Access properties:

The getfield () method of the class object can obtain the member variables of the class, and then use the two methods provided by the field object to read and set the member variables

 1. getXxx(Object obj) 2. setXxx(Object obj,Xxx val)

Call method:

The specified method can be obtained through getmethod () of class object, and a method object can be returned. Each method method corresponds to a method. Check its invoke () method to call the corresponding method

Specific demonstration

Create a class:

< pre class = "has" > public class Class1 {public int YYY = 0; public string name = "this is the name before the change";

}

Get class object:

 String classname = "Class1";  Class Clazz = Class. forName(classname);

Create object:

 Constructor c = Clazz. getConstructor();  Class1 aa =(Class1) c.newInstance();

Access and set member variables:

 Field f1 = Clazz. getDeclaredField("name");  f1. set(aa,"wanger");

Call method:

 Method m = Clazz. getmethod("setName",String.class);  m. Invoke (AA, "Li Si");

You can also configure the class to be loaded through an external file, change the class to be loaded by modifying the information in the file, and create a text file in the SRC directory, the content of which is:

 class=Service1

Obtain the class name by reading the external file class and load it with the obtained class name:

 File f1 = new File("C:\Users\kelis\IdeaProjects\Spring_A\src\spring.txt");  Properties config = new Properties();  config. load(new FileInputStream(f1));  String className = (String) config. get("class");  Class clazz = Class. forName(className);

More discussion:

The difference between reflection and mapping

Mapping is just a concept, which is usually expressed in code. Reflection is a technology that many high-level languages have. The specific function is to find other information of an independent object

What are the application scenarios for reflection

Reflection can be used to dynamically load the required classes without modifying the source code of the program. Reflection technology is also used in many frameworks, such as spring, which sets the classes to be loaded through the configuration file class

The function of reflection, or popular interpretation of reflection

The implementation of dynamic loading classes increases the flexibility of the program and is mostly used in various development frameworks

PPT:

It communication group 828691304 can be added for career selection, job counseling, learning planning, difficult Q & A, technical exchange, etc

Welcome to our website:

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you and stop being confused on the way to study.

Here is the skill tree It academy is a gathering place for beginners to switch to the Internet industry. "

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

Here is the skill tree · it Academy: nofollow "> https://www.jnshu.com , beginners switch to the gathering place of the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>