1: Loading of classes
When we use a class for the first time, the system does two things:
1: Load the class's bytecode file (. class) file into memory
2: The system also creates an object for the bytecode file: Class object
A.txt Hello.java d://lesson//a.txt---->file class
A.class hello.class demo.class---->class class
2: When a class is loaded
1. Creating an instance of a class
2. Static variables for a class, or assign a value to a static variable
3. Static methods for classes
4. Use reflection to force the creation of a Java.lang.Class object for a class or interface
5. Initialize subclasses of a class
6. Use the Java.exe command directly to run a main class
3: The effect of the reflection
1: Reflection allows us to compile with a non-existent or later to exist class
2: Reflection can access private members of a class MD5 algorithm
3: Through reflection, we write some common code
4: Steps of Reflection
1: Get Class object//A class has only one class object in memory
2: Gets the members of the class through the class object (constructor, member variable, member method)
3: Access to these variables and methods
Words:
Class
? Constructor Construction
? Method methods
? Field fields
? Instance instances
? Invoke execution
5: Get the members of the class through class
1: Get the constructor method for a class
A: Get all the constructs
Public constructor<?>[] GetConstructors ()//can only get public decorated constructs
Public constructor<?>[] Getdeclaredconstructors ()//can get all the constructs (including private)
B: Get a single construct
Public constructor<t> getconstructor (class<?> ... parametertypes)
Public constructor<t> getdeclaredconstructor (class<?> ... parametertypes)
2: Get Member method
A: Get all the member methods
Public method[] GetMethods ()
Public method[] Getdeclaredmethods ()
B: Get a single member method
Public Method GetMethod (String name, Class<?> .... parametertypes)
Public Method Getdeclaredmethod (String name, Class<?> .... parametertypes)
4: Get member variables
A: Get all the member variables
Public field[] GetFields ()
Public field[] Getdeclaredfields ()
B: Get a single member variable
Public Field GetField (String name)
Public Field Getdeclaredfield (String name)
The reflection of Java