Detailed explanation of java reflection mechanism examples
Detailed explanation of java reflection mechanism examples
1、 Is java a dynamic language?
Generally speaking, when it comes to dynamic language, it means that the program structure or variable type can be changed when the program is running. From this point of view, Java, like C + +, is not a dynamic language.
But Java has a very prominent dynamic correlation mechanism: reflection. Through reflection, Java can load, discover and use classes that are fully summed during compilation, generate their object entities, call their methods, or set values for properties at run time. So Java is a semi dynamic language.
Concept of reflection:
The reflection mechanism in Java means that all properties and methods of any class can be known in the running state;
Any method of any object can be called;
This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of Java language
2、 Dynamic properties
2.1 dynamic properties
Generate object instances at runtime; Calling methods during operation; Change properties at run time
2.2 functions realized by java reflection mechanism
Judge the class of any object at run time, construct the object of any class at run time, judge the methods and properties of any class at run time, and call the methods of any object to generate dynamic proxy at run time
2.3 java reflection applications
In Java programs, many objects have two types at run time: compile time type and run time type
The type at compile time is determined by the type used when declaring the object, and the type at run time is determined by the type actually assigned to the object
For example:
Compile time type is person and runtime is student
In addition, the program may receive an object passed in from the outside at runtime. The compile time type of the object is object, but the program needs to call the method of the object's runtime type. In order to solve these problems, the program needs to find the real information of objects and classes at run time. However, if the compiler cannot predict which classes the object and class may belong to at all, the program only depends on the runtime information to find the real information of the object and class. At this time, reflection must be used
3、 Java reflection API
The reflection API is used to generate information about classes, interfaces or objects in the current Java virtual machine.
Class: the core class of reflection, which can obtain the attribute, method and other content information of the class. Field class: Java lang.reflect. Represents the property of the class. You can get and set the property value in the class. Method class: Java lang.reflect。 Represents the method of the class, which can be used to obtain the information of the method in the class or execute the method. Constructor class: Java lang.reflect。 Represents the constructor of the class.
4、 Get all methods and properties
Person class
Use reflection:
Enter com pb. Reflect. classinfo. Person
result:
5、 To work with reflections
5.1 steps
Java. lang.reflect
Get the Java The lang. class object calls the methods of class and uses the reflection API to manipulate this information
5.2. Method of obtaining class object
Call the getClass () method of an object
Call the class attribute of a class to get the class object corresponding to the class
Use the class class's forname() static method
6、 The second method is the getClass () method of the object
The person class will construct the method public because the object is to be declared
Use reflection:
Test class
7、 The third method class Class attribute
Person class is the same as above
Test class:
result:
ditto
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!