Home > Article > Web Front-end > [Dynamic page] (2) Java reflection_html/css_WEB-ITnose
Java’s reflection mechanism is a very important feature of the Java language. Let’s first take a look at the Sun official website to see how the official website defines the reflection mechanism.
A rough translation, the translation may not be very accurate. Reflection is a feature of the Java programming language. It requires the Java program to check or self-check when it is running, and to operate on internal properties. For example, it allows a Java class to obtain all its own member names and display them. The ability to inspect and operate from within a Java class may sound unpopular, but this feature simply doesn't exist in other programming languages. For example, there is no way for Pascla, C, or C# programs to obtain functions defined within them. Another real-life example of reflection is in JavaBeans, where software components can be manipulated through constructors. This constructor uses reflection to obtain Java properties (classes) because they are dynamically loaded.
In other words, to understand Java’s reflection mechanism in a simple way, in the running state, any class can dynamically obtain the properties and methods of its class, and any object can call it. Either way. This function of dynamically obtaining information and dynamically calling object methods is called Java's reflection mechanism, and this mechanism is unique to the Java language and is not available in other programming languages.
Many open source frameworks now use reflection mechanisms, such as Hibernate and Struts, which are all implemented using reflection mechanisms. But why do we use reflection mechanism? Think about how we implemented it in the past. We determined a type during compilation, such as a class or an object, which was hard-coded and fixed, and then instantiated the object or called a certain method. This method is called static Compile. Then the counterpart is dynamic compilation, which is the Java reflection we talk about in this blog. Its implementation principle is to determine the type at runtime and then bind the object. This dynamic compilation feature maximizes the power of the Java language, enhances the flexibility of Java, and embodies polymorphic applications.
If you still don’t understand what is said above, then try another way. Java's reflection means that the program can obtain its own information when it is running. In Java, as long as the name of the class is given, then all the information of the class can be obtained through the reflection mechanism. Think about the reflection mechanism we use in OA because we are not sure which entity is passed in, whether it is Role, Apartment or User, so we use Java's reflection mechanism Class
Now that we have Java’s reflection mechanism, we have to break out of the old circle. Thinking about how smart humans are, it’s enough to just develop tools and think about how to achieve high reusability and efficiency through continuous improvement. So now we use Java's reflection mechanism to only create a Dao class, and then write the methods of adding, deleting, modifying, and checking into it, but leaving the type empty. Then we only need to pass in different types of objects to operate, and there is no need to have many tables. No matter how many Dao classes we create, the reflection mechanism will automatically do this for us. It's like there is a wine cellar with three wine tanks, containing blue wine, green wine and yellow wine respectively. The owner of the wine cellar puts a kind of wine tank at the door every day to sell wine. The previous practice was: for example, if you sell blue wine today, you will bring the blue wine jar and place it at the door; if you buy green wine, you will move the green wine jar to the door. Later, a guy came up with an idea, which was to put an empty wine vat at the door and put a faucet on it. If you want to sell any wine today, just pour it into the wine vat at the door, so you don't have to carry the wine vat back and forth. . Haha, what a stupid boss, of course I made this story up, just to explain the reflection mechanism of Java.
So looking back, Java The reflection mechanism can realize dynamic creation of objects and compilation. For a better understanding, let's make up another example. Now I am a class teacher. I used to assign work like this. Xiaohong is responsible for sweeping the floor; Xiaolan is responsible for cleaning the glass; Xiaohei is responsible for cleaning the blackboard; and Xiaohuang is responsible for taking out the trash. Everyone is only responsible for their own work. To sweep, you have to remember to arrange for Xiaohong to sweep the floor...
But this allocation of work is very inefficient, because I always think about arranging For a job, you have to think about which fixed person to do it. So with the Java reflection mechanism, I changed the way I arranged work in the past.
In this way, using Java’s reflection mechanism, you don’t know which object you want to new to do what work. It used to create objects directly to perform the corresponding work. Now, using Java’s reflection mechanism, during the running process of the program , dynamically create an object and then call any of its methods and properties to perform its functions. I don’t know if you understand Java’s reflection mechanism in this way? In the next article, I will introduce how to read the class name and attributes of the Jar package through Java's reflection mechanism, so stay tuned!