-------Android Training, Java training, look forward to communicating with you! ----------
Reflection
Reflection can obtain all of its internal property objects and method objects as long as it knows its class object. The class object can be obtained as long as the instance object of its class is known.
Class. Unlike the keyword class, which is the encapsulation of classes in Java, is an abstract description of all classes, and a specific class is its instance object. The class-type object obtained by the instance object of the same class is the same object that is instantiated by the same byte-code address class file.
The method of getting a class name is actually a class-type object that gets the class instance object, and this object is the class, but the content for that class also contains the byte-code address of the class. Classes have classes, and members of classes also have corresponding classes. Class in a class that encapsulates the method that gets the object of the member class in the class. As the following object of the construct class is a constructor in the class, you can get the class object of the class, that is, the class object of the class, and call the method in it or the object in the class construct class, which takes the constructor as its content. The object-oriented thought is fully embodied here, and everything is object.
GetClass ();
Class.forName ("");
The acquisition method of eight basic data types, int.class; String.class;void is also a class object void.class; These are an instance object class of Class
The judgment of the class name
Isprimitive (); Whether it is a basic type, such as String.class.isPrimitive () returns false;string is not a basic data type
Comparison of wrapper class types with basic data types, such as Int.class==interger.type; (the latter is a static field encapsulated in a class)
Class.isarray (); Determines whether a class is an array.
In short, as long as there are types that appear in the source program, there are individual class instance objects.
Reflection is the mapping of the various components in the Java class into the corresponding Java classes. The individual members of the class are abstracted into the corresponding Java classes.
such as Field,method,contructor,package, etc.
The constructor class is the construction method class.
Constructor[] GetConstructors (); Gets all the constructor object, returning an array.
Constructor getconstructor (param); Gets a constructor method object, which is the parameter type class object
Gets the constructed method object and then obtains a new object instance through Newinstance (Initargs), with the parameter specifying the initialization parameter
such as String.class.getConstructor (Int.class);
Member variables Field class
int x=5;
Field Fieldy=person.getclass (). GetField ("X"), where the value of Fieldy is not 5, but the byte code value of x. Like the variable x as the object, and field is his class, Fieldy is a reference to the variable x object, x value is the content of the X object, the value of Fieldy is the address of the object x, compared to = = comparison is the address, equals rewrite after the comparison is the content, here fieldy!== X, because = = compares the two ends of the former is the address value, the latter is the X value of the content variable x of the object so it is not equal.
Calling the main method of another program according to the class name
The class object is obtained first, and then through the GetMethod method or the Methods class object.
The Innvoke method in the Methods class object begins the call.
Reflection of an array
Arrays are also a type and have class class classes. Relationship to the object class
The array class can be obtained, and there is a arrays tool class which has methods for manipulating the arrays of objects. This allows the array class to be obtained with reflection and directly manipulate the array through the arrays class, such as traversing without having to write the For loop.
The reflection is practical, realizes the frame function. That is, you do not know the class you want to call beforehand, and after you have written the framework, use the reflection technology to invoke the class that you added afterwards by using a pre-configured file. The class that you add afterwards only needs to conform to the configuration file, in combination with the pre-written framework.
Dark Horse programmer _java reflex