[introduction to Java] day13 reflection mechanism in Java

I've been busy some time ago, so I don't have much time to write a blog. It's time to update it after such a long delay. Recently, I have seen the introduction of various knowledge payment. I feel that it is a good thing and a bad thing. A good thing is the recognition and promotion of knowledge precipitation. A bad thing is that many people are busy realizing their knowledge. Relatively, they are not doing enough in precipitation. I don't have any ideas about this for the time being. I always feel that it will be faster to take it slow and master the rhythm.

All right, back to business.

Reflection mechanism is a very powerful feature in Java. It can obtain class information at runtime, such as class parent class, interface, all method names and parameters, all constants and variables. It can be said that the class has been naked in front of reflection (cough, this is a regular car). Let's take a little chestnut and feel it at will:

The output of calling testa method is as follows:

Here, all methods and variables of string type are obtained, and only the full name of string type is used. Of course, the function of reflection is not only to obtain class information, but also to dynamically create objects at runtime. Recall that our normal use of objects requires declaration in the code before they can be used. However, after using reflection, we can dynamically create objects and call their methods during runtime, and even directly view the private member variables of the class, It can also obtain the annotation information of the class, which is often used in type judgment in generics. Reflection can be said to completely break the encapsulation of the class and expose all the class information.

It doesn't matter if you don't understand the above code. Just feel the ability of reflection a little. After introducing what reflection can do, this tutorial will no longer write some toy code. This time, a practical code is used as the medium to introduce reflection.

In development, we often encounter replication between two different objects. Get the field information in one class and then set it to another class. In most cases, the fields corresponding to the two classes are the same. It is very troublesome to use each time, so we can realize a encapsulation by using reflection, Simple class field replication can be achieved by calling only one method.

So, let's think about it first. To copy all the field information of one class object to another class object, first, how to get the value of a field of a class? Let's write a method first:

Two classes not mentioned before are used here. One is class. Does it look familiar? Think about it. Do we use it every time we define a class? Ha ha, you are wrong. That is the class keyword. Java is case sensitive. Class here is a class name. What is this class used for?

When the virtual machine loads each class, it will automatically generate a corresponding class class to save the class information. It can be understood that class class is the proxy class of that class and the bridge between the actual class and the class loader. It can be used to obtain the class loader reference of the virtual machine, so as to realize more operations. Class is a generic class. Each class has a corresponding class. For example, the class class corresponding to string is class < string >.

Class has many methods to obtain more information about the class. Here, the getdeclaraedfield method is used to obtain the information of the specified field. The field type object is returned. This object stores some information about the field, such as field name, field type, field modifier, field accessibility, etc. the setaccessible method can set the accessibility of the field, In this way, you can directly access the private modified field, and then use the get method to obtain the value of the corresponding field of the specified object.

Let's test:

The output is as follows:

Next, we need to get all the fields in the class, and then find out whether there is a corresponding field in another class. If so, set the value of the field to the corresponding field.

The output is as follows:

Perfect. In this way, we use the reflection mechanism to perfectly copy the same fields between objects of different classes. There are only two fields here, so the benefits may not be obvious. In fact, in actual development, there are often operations to convert Bo into vo. At this time, this operation is very necessary, A simple command line can replace a lot of get and set operations.

Of course, the use of reflection mechanism is high-end and high-grade, but it is also a double-edged sword. Improper use is likely to bring serious consequences. Moreover, the use of reflection will occupy more resources and reduce the operation efficiency. The above tools use operation efficiency for development efficiency. It is not recommended to use a large number in development. In the same sentence, technology is only a means and can be used when it needs to be used. Do not use it for use.

As for other methods and postures in reflection, we can try our best to explore slowly. Here is just a brick to attract jade.

So far, the explanation of this article is completed. Welcome to continue your attention.

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