Java reflection comprehension and Java reflection Comprehension
Reflection: Get the class when the object is running.
Person. class --> Person object Class. class --> Class Object
We create a Person object through the Person. class bytecode file to represent a real Person. The Person object is used to map a Person. It is not a bytecode file on the hard disk, but a Person object created based on the loading class in the memory. The loaded Class is actually a Class object. It only contains static members.
We use Class. the class bytecode file creates a Class object that represents a Class in reality. The class object is used to map a Class. It is not a bytecode file on the hard disk, instead, the bytecode information is encapsulated into an object in the memory, which is a Class object. This object maps to a class.
------------------------------------------------------------------
The completion process of Person p = new Person () is as follows: first find the Person. class bytecode file on the hard disk. Then, the bytecode information is encapsulated into a Class object in the memory. The object type is a Class object, referred to as the loading Class, and then a Person object is created through the Class object.
Note: Each object (NEW object) corresponds to a Class Object (loading CLass). Different Class objects represent data types not used in JAVA. All classes in JAVA are encapsulated into class objects.
Public static void main (String [] args) throws Exception {// Person. class --> Person object --> zhangsan // Class. class --> Class Object (indicating a type) --> Person. classObject obj = new Person ("zhangsan", 19); // There are three methods to obtain the object type/* 1. if the object Class clazz = obj is obtained. getClass (); // returns an Instance Object of the class type. String classname = clazz. getName (); System. out. println (classname); * // * 2. if the String classname = "cn. itcast. day9.reflectstudy. person "; Class clazz = Class. forName (classname); System. out. println (clazz. getName (); * // * 3. clearly know which type of Class Object To take Class clazz = Person. class; System. out. println (clazz. getName (); System. out. println (int. class. getName (); */Class clazz = obj. getClass ();/* traverse all attributes of the class Field [] fields = clazz. getDeclaredFields (); for (int I = 0; I <fields. length; I ++) {String name = fields [I]. getName (); Class type = fields [I]. getType (); System. out. println ("property name:" + name + "property type:" + type. getName ();} * // obtain an attribute and access this attribute Field field = clazz. getDeclaredField ("name"); field. setAccessible (true); // The access check field is canceled. set (obj, "Zeng ge"); System. out. println (obj); Object value = field. get (obj); System. out. println (value); * // * obtain the run Method of the object, and call Method m = clazz. getDeclaredMethod ("run", null); m. invoke (obj, null); * // * obtain the class Constructor construction method, and create the class instance */Constructor con = clazz. getConstructor (String. class, int. class); Object object = con. newInstance ("zhangsan", 29); Person p = (Person) object; System. out. println (p );}