Reflection mechanism of Java

It's hard to understand what this is, for example.

Suppose we have a picture of animation now. Through this picture, we search for relevant memory in our brain to know what this picture is and who in the animation; If we now have a name and search for relevant memory in our brain, we will naturally think of the specific image of the person with that name. This thinking process is reflection.

Assuming that a class has been loaded into the JVM, given the class name, we can obtain the corresponding information of the class through reflection.

Specific definition: reflection is the key to being regarded as a dynamic language. The reflection mechanism allows the program to obtain the internal information of any class with the help of the reflection API during execution, and can directly operate the internal properties and methods of any object.

Functions provided by java reflection mechanism:

Reflection related APIs:

One. Class class

The following methods are defined in the object class, which will be inherited by all subclasses: public final class getclass().

The return value of the above method is a class class, which is the source of java reflection. In fact, the so-called reflection is also well understood from the running results of the program, that is, find the name of the class through object reflection.

Reflect the information you can get: the properties, methods, constructors of a class, and which interfaces a class implements. For each class, the JRE reserves an object of type class for it. A class object contains information about a particular class.

There are four ways to instantiate class:

2、 Call the complete structure of the class through reflection

Person. java

Move. java

Study. java

Student. java

test. java

1. Get the parent class and interface of the class

Parent class: ref.person interface name: ref.move interface name: ref.study

2. Get the construction method of the class

--------------------------Construction method: ref.student construction method modifier: 1 ---------------------------------------------- construction method: ref.student construction method modifier: 1 construction method: the parameter type of ref.student is Java lang.String --------------------------

--------------------------Construction method: ref.student construction method modifier: 1 ---------------------------------------------- construction method: ref.student construction method modifier: 2 construction method: the parameter type of ref.student is Java Lang.string constructor: the parameter type of ref.student is int -------------------------------------------------------- constructor: ref.student constructor modifier: 1 constructor: the parameter type of ref.student is Java lang.String --------------------------

3. Create objects by reflection

Student () is called, student (string school) is called, and student (string name, int age) Jack 23 is called

4. Method of obtaining class

--------------------------Method name: showinfo return value type: void modifier: 1 ---------------------------------------------- method name: study return value type: void modifier: 1 ---------------------------------------------- method name: move return value type: void modifier: 1 ------------------------------------------------------------------ ------Method name: testgetschool return value type: class java Lang. string modifier: 1 ---------------------------------------------- method name: wait return value type: void modifier: 17 ---------------------------------------------- method name: wait return value type: void modifier: 17 method parameter type: long method parameter type: int ------------------------------------------------- Method name: wait return value type: void modifier: 273 method parameter type: long -------------------------------------------------------- method name: equals return value type: Boolean modifier: 1 method parameter type: Java Lang. object -------------------------------------------------------- method name: toString return value type: class java Lang. string modifier: 1 ---------------------------------------------- method name: hashcode return value type: int modifier: 257 ---------------------------------------------- method name: getClass return value type: class java Lang. class modifier: 273 ---------------------------------------------- method name: notify return value type: void modifier: 273 ---------------------------------------------- method name: notifyAll return value type: void modifier: 273--------------------------

--------------------------Method name: test return value type: void modifier: 2 method parameter type: Java lang.String --------------------------

--------------------------Method name: showinfo return value type: void modifier: 1 ---------------------------------------------- method name: study return value type: void modifier: 1 ---------------------------------------------- method name: move return value type: void modifier: 1 ------------------------------------------------------------------ ------Method name: testgetschool return value type: class java Lang. string modifier: 1--------------------------

5. Get the properties of the class

--------------------------Attribute name: School attribute type: class java Lang. string modifier: 1 ---------------------------------------------- property name: Name property type: class java Lang. string modifier: 1 ---------------------------------------------- property name: age property type: int modifier: 1--------------------------

--------------------------Attribute name: School attribute type: class java Lang. string modifier: 1 ---------------------------------------------- property name: privatefield property type: class java Lang. string modifier: 2--------------------------

6. Get the package of the class

ref

3、 Call the methods and properties specified in the class through reflection

To student Insert the following methods into Java:

1. Call the method specified in the class

Student () is called, which is the public setinfo (string name, string school) method, which is the private test (string name) method, which is in the public setinfo (int age) method 3

2. Call the properties specified in the class

The private property of student () is called

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