Java uses reflection

There are two types of objects in Java programs at run time: compile time type and run time type. For example, list = new arraylist(). The compile time type of variable list is list, and the runtime type is ArrayList. There are more extreme types. For example, if an object is passed through network communication, the compile time type of the object is object, but the program runtime needs to call the method of the object runtime type. In order to solve this problem, the program needs to find the real information of objects and classes at run time. In order to solve this problem, there are two methods:

Get class object

After each class is loaded, the system will generate a corresponding class object for the class, and the class in the JVM can be accessed through the class object. There are three ways to obtain class objects in Java programs:

Both the first method and the second method obtain the class object of the class directly according to the class. In contrast, the second method has the following two advantages:

That is, in most cases, we should use the second method to obtain the class object of the specified class. However, if there is only one class name string, only the first method can be used. Once the class object corresponding to a class is obtained, the program can call the method of the class object to obtain the real information of the object and the class.

After obtaining the class object of the class, you can obtain the constructor, method, attribute, comment, anonymous inner class and other attributes of the class through the class object.

Use the class object to create the object of the class

There are two ways to generate objects by reflection:

Calling methods using class objects

Class objects can use getmethods () or getmethod () to get all or the specified methods. These two methods return an array of method objects or method objects.

After obtaining the method object, you can call the corresponding method through method.

Private methods are also accessed through the setaccessible method, but the method to obtain the method object needs to be adjusted.

Accessing member variables using class objects

You can obtain all field properties or specify fields included in the class through the getfields() method or getfield() method of the class object. Field provides the following two methods to access properties:

Getxxx (object obj): get the field attribute value of obj object. XXX corresponds to 8 direct types. You can use get directly for reference types.

SetXXX (object obj, XXX, Val): set the value of the field of obj object to val. XXX corresponds to 8 direct types. You can use get directly for reference types.

The example code is as follows:

Use reflection to manipulate arrays

In Java An array class is also provided under the lang.reflect package, and the array object can represent all arrays. The program can dynamically create an operation array by using array.

Use code to illustrate the following:

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