In Java, everything is an object
The Java reflection mechanism is to get the properties and methods of any class in the program running. To class.forname ("Com.mysql.jdbc.Driver.class"), I believe that everyone will not be unfamiliar, until the study of the Java reflection mechanism, it dawned, the original database connection used in this sentence is the Java reflection mechanism.
There are three ways to get a class object:
Class c=class.forname (""); This is also the first time you have touched the Java reflection mechanism while learning Factory mode, and then viewed the Java reflection mechanism from the Internet.
Second, Class c=new String (""). GetClass ();
Third, Class C=shape.class
Classes required for class reflection in Java field, Constructor, Method, class, Object
Field class: Provides information about the properties of a class or interface, and the dynamic access rights to it. The reflected field may be a class (static) attribute or an instance property, and a simple understanding can be seen as a class that encapsulates the properties of a reflective class.
Constructor Class: Provides information about a single construction method of a class and access to it. Unlike field classes, the field class encapsulates the properties of the reflection class, while the constructor class encapsulates the method of constructing the reflection class.
Method Class: Provides information about a single method on a class or interface. The method that is reflected may be a class method or an instance method, including an abstract method. This class is not difficult to understand, it is a class used to encapsulate the reflection class method.
Class : An instance of a class represents classes and interfaces in a running Java application. An enumeration is a class, and a comment is an interface. Each array belongs to a class that is mapped to a class object, and all arrays that have the same element type and number of dimensions share the class object.
Object class: Each class uses object as a superclass. All objects, including arrays, implement methods of this class.
use of the Java reflection mechanism
- Determine the class to which any object belongs at run time
- To construct an object of any class at run time
- Determine the member variables and methods that any one class has at run time
- Methods to invoke any object at run time
Initial knowledge of Java reflection mechanism