Reflection:
The reflection mechanism of the Java language:
Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class;
For any object, it can call any of its methods and properties;
This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.
How to dissect a class:
To dissect a class, you must first obtain the bytecode file object for that class.
The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.
In layman's terms:
Reflection: It is through the class file object, to use the member variables in the file, construction methods, member methods.
Person p = new person ();
P. Using
To Use this, you first have to get the class file object, which is actually the object of class.
Class:
Member Variable Field
Construction Method Constructor
Member method
How to get the class file object:
1:person p = new person ();
Class C = P.getclass ();
2:class C2 = Person.class;
Any data type has a class static property that looks simpler than the first.
3: Pass the class masterpiece as a string to the static method in class Forname
Class C3 = Class.forName ("person");
The difference between the third and the first two types
The first two you have to be clear about the type of person.
The following is the type of string that you and I have. This extension is stronger. I don't need to know your class. I only provide strings, which can be loaded according to the configuration file.
Who do we use in general?
A: Play any one of your own, the second is more convenient
B: Development of a third
Because the third type is a string, not a specific class name. This allows us to configure such a string in the configuration file. Variable
First, build a person class to be used for dissection.
The anatomy of the Code embodies:
1 Public classRelfectdemo {2 3 Public Static voidMain (string[] args)throwsClassNotFoundException {4 5 //Mode 16person P1 =NewPerson ();7Class C1 =P1.getclass ();8 9person P2 =NewPerson ();TenClass C2 =P2.getclass (); One ASystem.out.println (P1 = = p2);//false opens up 2 different spaces -SYSTEM.OUT.PRINTLN (C1 = = C2);//True 2 classes are getting the same bytecode file object - the //Mode 2 -Class C = person.class; - //Int.class; - //String.class; +System.out.println (c = = C1);//true - + //Mode 3 class.forname (className); The file name inside is the full path. A // There are two ways to get the file name in this method : at //One: Write Zl_relfect.person outside (package name plus class name), and then copy and paste in - //Second: Right click on the desired class, click Copy qualified Name, then paste in -Class C3 = Class.forName ("Zl_relfect.person"); -System.out.println (C3 = = C1);//true - } - in}
Overview of the reflection of the Java 27-2 reflection and how to get the class file object