Briefly summarize the application of java reflection mechanism

Java's reflection mechanism is a very key mechanism to make it dynamic, and it is also a feature widely used in JavaBeans.

In short, a class or an object has the following attributes: method, constructor and field. The general structure class diagram is as follows:

Let's illustrate the problem with code:

First, let's look at class. In class, we can see the following important methods;

This is used to get the interface or superclass of a class and judge whether the class is an interface;

And the following methods:

Obtain the corresponding constructor, field and method of this class;

The difference is that methods with declared can get all the attributes declared by this class, while those without declared can only get public attributes;

The inherited member is given to the three classes respectively, and the class that declares it can be obtained, using getdeclarangclass(); Here we introduce modifiers again. We all know that there are several modifiers in front of field or constructor and method, such as:

Wait, we can get an integer value of this modifier by using getmodifiers (), and then apply the static method of modifier class to judge; For example, modifier Isstatic (int) and so on;

Next, let's take a look at the main methods of accessibleobject. The first is getannotation(); Get the comment of an attribute; isAccessible(); Accessibility; If it cannot be accessed, we can use setaccessible (Boolean) to set its accessibility; (we have seen this in spring);

Then let's take a look at some important features of the three classes:

Constructor:

Field:

Method:

Here is an example code:

console:

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