JAVA21
Loading of class loader classes
When a program is to use a class, if it has not been loaded into memory, the system will initialize the class by loading the connection initialization three steps.
L Load
- The class's loader reads the class file into memory, puts it into the method area, and creates a class object for it
- is an object of the compiled class file
- Object into heap memory
- Also called bytecode object
- You can use not to create
L Connection
- Verify that you have the correct internal structure
- Allocating space for static members and setting default initialization values
- Parsing converts a symbolic reference in a class's binary data to a direct reference such as a=1 in memory directly to 1 without the symbol a
L Initialization
is to create the object
Load time of Class
L Create a new class with the Main method
L Other classes call method member variables in this class or instantiate create objects
L Other classes call method member variables or instantiate objects in subclasses of this class
Running any class is a way to run the object class first
=
The composition of the loader for the class
L Root Class Loader
- Responsible for loading the core class library
- Rt.jar in the JRE directory
- Rt.jar in Jresystemlibrary in the project
L Extension Loader
- To load an extended class
- Ext folder in the JRE
- The jar package in the Ext folder in Jresystemlibrary in the project
L System Loader
- Be responsible for loading the classes you write
- Responsible for loading third-party tool classes
Reflection concept
L run time for any one class
L can all know all the properties and methods of this class.
L can invoke any of his methods and properties for any object
The function of this dynamic method of acquiring information and dynamically invoking objects is called the reflection mechanism of the Java language.
Function
- A class that has been completed if you want to add functionality without changing the source of the premise of adding
- Dissecting the bytecode file of the new feature class, which is the compiled class file
- Called in the source class.
The process of creating a class file
L class loader loading a new feature class produces a class file object
L method of invoking the new function class with this file object
The class file object of the Java.lang.Class class describing the file object is the type of this class
Three ways to get a class file object
L Object Acquisition
L Class name get
L Class-Class static method acquisition
L Three methods The objects obtained are the same
Reflection gets the construction method in the class file
L Run Construction method is equivalent to creating an object
L GetConstructors () gets all the common construction methods in the class file object
L returns an array of type constructors
L Gets a specified constructor method
Variable parameters
L Run
Newinstance ()
L get the constructor method with parameters and run
L Quick Way
- The reflected class must have an empty parameter construction method
- The construction method must be public
Reflection gets private Construction method
L do not recommend damaging the package security of the program
L Getdeclaredconstructors ()
L Get all the construction methods
L returns an array of type constructor
l Example
L Getdeclaredconstructor (class.. C
L Variable Parameters
L return a private method of construction
l Example
Cannot access private
L Setaccessibale access to permissions
Reflection Get Member Variable
The method in Class GetFields () Getdeclaredfields () is to obtain private
- Gets the member variables for all public public classes
- return value field[] Array
L GET the specified member variable
Reflection get Null Parameter member method
L GetMethods ()
- Gets all the public member methods in the class file, including the inherited
- The method class is an object that describes member methods
Many of the methods are public.
L GetMethod ()
- Method parameter method name and variable parameter
- Returns a Method object
L Use the Obtained method
Invoke ()
- Methods obtained by running method
- parameter is an object of a class file
Reflection gets the member method with parameters
Reflection-Generic Erase
L store elements outside of the specified generics
l pseudo-Generic compiled class file without generics can get the class file object get method directly
L cannot use strong for or iterator traversal so just to practice reflection
Through the configuration file
L principle
L Implementation
The Java self-study road-day21