Basic use of 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 reflection]

[java small class of Xiuzhen academy] basic use of reflection

Hello, I'm Zhao Linai, a student of the fourth phase of Xi'an Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the Java task 1 on the official website of the Academy, the knowledge point in deep thinking - the basic use of reflection

(1) Background:

What is reflexivity

Reflection, it can be seen from this anti word that it must be different from our normal use logic, so what's different? If you want to understand "anti", you must first understand the concept of positive.

Under normal circumstances, if you want to use a class, you must go through the following steps:

(1) Use important to import the package where the class is located (class: Java. Lang. class)

(2) Instantiate class objects through the keyword new (construction method: Java. Lang. reflect. Constructor)

(3) The generated object can use "object. Attribute" to call the attributes in the class (attribute: Java. Lang. reflect. Field)

(4) Call the method in the class through "object. Method ()" (method: Java. Lang. reflect. Method)

In reflection, using a class does not need to import the package of the class. As long as you know the full path of the class, you can know all the information in the class.

Reflection does not need to have an explicit type object. All objects are represented by object. You can directly call the methods in the class with the mixture of object and reflection mechanism

Java reflection is to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; And can change its properties. This is also a key property of the language that Java is regarded as dynamic (or quasi dynamic, why should we say quasi dynamic, because generally speaking, the definition of dynamic language is that the program runtime allows the change of program structure or variable type. This language is called dynamic language. From this point of view, Perl, Python and ruby are dynamic languages, while C + +, Java and c# are not dynamic languages).

(2) Knowledge analysis:

Reflection with

Three ways to get class objects

1. Object ——> getClass();

Through getClass () method: (through object)

2. Get object through object instance method: (through object)

3. Full path of class: (by class name)

(3) Frequently asked questions:

Advantages and disadvantages of reflection

(4) Solution:

Of course, the advantage of reflection is reflected in its dynamic nature. It can determine the type and bind objects at runtime. Dynamic compilation maximizes the flexibility of Java, reflects the application of polymorphism and reduces the coupling between classes. In a word, the advantage of reflection mechanism is that it can realize dynamic object creation and compilation, especially in the development of J2EE, its flexibility is very obvious. For example, a large-scale software cannot be designed perfectly at one time. When the program is compiled and released, when it is found that some functions need to be updated, we can't ask users to uninstall the previous software and reinstall the new version. If so, this software must not be used by many people. If it is static, the whole program needs to be recompiled once to realize the function update. If it adopts the reflection mechanism, it can realize the function without unloading. It only needs to be dynamically created and compiled at runtime.

Its disadvantage is that it has an impact on performance. Using reflection is basically an interpretation operation. We can tell the JVM what we want to do and it meets our requirements. Such operations are always slower than just performing the same operation directly.

(5) Coding practice:

Try to find what is used in real projects. If you can find a website using your knowledge, this is the best. Apply what you have learned, otherwise it will be meaningless to take it as an exercise. Prepare more demos and combine the knowledge points with the demo during the explanation to facilitate everyone to understand the knowledge points explained.

(6) Expand thinking:

Java reflection applications

In Java programs, many objects have two types at run time:

Compile time type and runtime type

The type at compile time is determined by the type used when declaring the object,

The type of runtime is determined by the type actually assigned to the object

For example: person P = new student();

Compile time type is person and runtime is student

In addition, the program may also receive an object passed in from the outside when running. The compile time type of the object is object,

However, the program needs to call the method of the object runtime type. In order to solve these problems, the program needs to find the real information of objects and classes at run time.

However, if the compiler cannot predict which classes the object and class may belong to, the program only relies on the runtime information to find the real information of the object and class,

You must use reflection

Finally, a summary

Reflection is simply to load objects dynamically and analyze them.

Java's reflection mechanism means that you can know all the properties and methods of any class in the running state;

Any method of any object can be called. This function of dynamically obtaining information is called the reflection mechanism of Java language

(7) References:

(8) More discussion:

Q1: rubrian: the relationship between IOC and reflection in spring

The implementation principle of IOC in spring is factory mode plus reflection mechanism. When adding subclasses to a general factory class, you need to modify the factory class. The factory mode using reflection mechanism can obtain the instance of the interface through reflection, but you need to pass in the complete package and class name. Moreover, users do not know how many subclasses an interface can use. We need to configure all subclasses in the form of property files.

Q2: Qiao Mingzhen: application scenario of reflection mechanism

A2: Zhao Linai: reverse code, such as decompilation

Framework combined with annotation

Simple reflection mechanism application framework

Dynamically generate class framework.

Q3: rubrian: what are the benefits of reflection?

A3: Zhao Linai: improve the flexibility and expansibility of the program, reduce the coupling line and improve the adaptability. It allows the program to create and control the object of any class without coding the target class;

(9) Thanks:

Thanks to elder martial brothers Liu you and Qiao Mingzhen. This tutorial is based on their previous technology sharing.

(10) Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

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
分享
二维码
< <上一篇
下一篇>>