Work encountered, problem solving:
Reflection mechanism in the Java language:
In the Java Runtime environment, for any class, can you know what the properties and methods of this class are?
Can I call his method for any of the objects? The answer is yes, this dynamic acquisition of the information of the class, and the ability to dynamically invoke the method of the class comes from the reflection of Java. This enables Java to have dynamic language features.
The Java reflection mechanism mainly provides the following features:
1. Determine the class to which any object belongs at run time
2. Constructing an object of any class at run time
3. At run time, determine the member variables and methods that any one class has (through reflection can even call the private method)
4. Methods to invoke any object at run time (* * * * NOTE: The premise is at run time, not at compile time)
Introduction to Java Reflection-related APIs:
In Java. Lang. Reflect in Bag
--class class: Represents a Class
--filed class: A member variable representing a class
--method class: Methods for representing classes
--constructor class: A method of constructing a representative class
--array class: Provides a static method for dynamically creating an array, and for accessing the elements of an array. All methods in this class are static methods
----Class
In the Java object class, there are a number of methods that should be rewritten in all Java classes:
Hashcode (), Equals (), Clone (), toString (), GetClass (), and so on, where GetClass () returns Yige
The object of class type.
Class is very special, and it inherits from object like the general class, in fact the body is used to express the Java program running
Class and interface, also used to express Enum,array,primitive,java Types and the keyword void
, when a class is loaded, or when the defineclass () of the Loader (class loader) is called by the JVM, it produces a class
Object
Class is the origin of reflection, and for any class you want to explore, it is only now that he produces a class
Object, the next step is to evoke more than 10 reflection APIs through the latter.
Java allows us to generate the corresponding class object from a variety of sources for a class.
--Using GetClass (): Methods in the object class, each class has this method
String str= "ABC";
Class Cl=str.getclass ();
-Use Class. Getsuperclass (): A method in class that returns the class of the class's parent class
-Use Class. Forname () static method:
--Use, class: name.
--Using the type syntax of primitive wrapper classes: The type of the basic wrapper class, such as: Integer.type
Note: The use of type is only suitable for native (basic) data types
Generate Instance----Run time
To create an object's entity, there are two methods in the reflection dynamic mechanism, one for the non-variable construction method and one for the parameter-oriented
Construction method, if you want to invoke the constructor with the parameter, it is troublesome to call the Newinstance () in the class directly.
, instead of calling the Newinstance () method in the constructor class, first prepare a class[] as the constructor parameter type.
Then call the Class object's GetConstructor () method to get an exclusive constructor object, and finally prepare a
Object[] As an argument to the constructor object's Newinstance () method.
What needs to be explained here is that only two classes have the Newinstance () method, respectively, class and constructor classes
The Newinstance () method in class is without parameters, while the Newinstance () method in the Constructro class is a parameter
Need to provide the necessary parameters.
Cases:
Class c=class.forname ("Dyntest");
Class[] Ptype=new Class[]{double.class,int.class};
Constructor Ctor=c.getconstructor (PTYPR);
Object[] obj=new object[]{new Double (3.1415), new Integer (123)};
Object object=ctor.newinstance (obj);
System.out.println (object);
Example 1:
Logininterface = defultclassloader.getclassinstance (new Class[]{hehe.class, Haha.class}, New Object[]{hehe, HAHA});
T client = (t) classtype.getconstructor (classes). Newinstance (objects);
Calling method----Run time
This action first prepares a class[]{} as the parameter type of the GetMethod (String name,class[]) method, and then prepares a
Obeject[] Place the argument and call the method object's Invoke (object obj,object[]).
Notice that the call here
Calling field content----runtime
Change field does not require parameters and arguments, first call class's GetField () and specify the field name, after obtaining a specific Field object
You can call field's get (object obj) and set (object Obj,object value) method directly
On the Java reflection mechanism