I. The concept of reflection:

It mainly refers to the ability of the program to access, detect and modify its own state or behavior, and adjust or modify the state and related semantics of the behavior described by the application according to the state and results of its own behavior.

Reflection is a powerful tool in Java that makes it easy to create flexible code that can be assembled at runtime without the need for source linking between components. But reflection can be expensive to use incorrectly!

Ii. The role of reflex mechanism:

1, decompile:.class–>.java

2. Access properties, methods, constructors, etc. of Java objects through reflection mechanism;

This is a little bit easier to understand, but let’s see how to implement these functions.

Third, Sun provides us with the classes of reflection mechanism:

java.lang.Class;

java.lang.reflect.Constructor; java.lang.reflect.Field;

java.lang.reflect.Method;

java.lang.reflect.Modifier;

Many of the methods, properties and other operations in reflection can be queried from these four classes. Or which sentence to learn to constantly query API, that is our best teacher.

Iv. Specific function realization:

1. There are three ways for reflection to get classes. Let’s get Employee

2. Create object: After obtaining the class, we create its object using newInstance:

3. Get attributes: divided into all attributes and specified attributes:

1) Get all the attributes:

2), acquire specific attributes, compare with traditional methods to learn:

4, get method, and construct method, not detailed description, just look at the keyword:

So we can get the various contents of the class and decompile it. For a compile-before-run language like Java, reflection makes code more flexible and object-oriented.

To sum up, the re-learning of Java reflection and the flexible use of it can make our code more flexible, but it also has its disadvantages, that is, the use of it will reduce the performance of our software and increase the complexity, so we need to use it carefully.

Five, Java learning video:

Java300: The new Java300 is coming! Java zero basic white self learning Java essential quality tutorial

Java zero Basic introduction to the full set of tutorials _ suitable for 0 basic introduction _JavaSE_Java basic tutorial