What is the reflection mechanism?
As a simple example, if you already have a class, you can certainly create an object from a class, and if you now require the name of a class to be found through an object, you need to use a reflection mechanism.
Normal Mode:
Introduce the required "package. Class" Name--and get instantiated objects with new instantiation--–>
Reflection mode;
Instantiate Object--–>getclass ()--–> Get the Complete "package. Class" Name
Below is a brief demonstration of the following code:
1. Calling the constructor method
PackageCOM.MM;/ * * Reflection * Call construction Method */ImportJava.lang.reflect.constructor;class person{intAge String name; Public Person(intAge,string name) { This. Age = Age; This. name = name; }@Override PublicStringtoString() {return "Name:"+ This. name+", Age:"+ This. Age; }} Public class fanshetest { Public Static void Main(string[] args)throwsException {class<?> Class1 = Class.forName ("Com.mm.Person"); constructor<?> Constructor = Class1.getconstructor (int. Class,string.class); Person per = (person) constructor.newinstance ( +,"Little Over"); System.out.println (per); }}
2. Calling the Normal method
Package Com.mm;import Java.lang.reflect.Method;/ * * Reflection * Call the normal method in the class */Class person1{PrivateString name; Public void SetName(String name) { This. name = name; } PublicStringGetName() {returnName }} Public classPutongmethod { Public Static void Main(string[] args) throws Exception {Stringstring="Name"; class<?> Class1 = Class.forName ("Com.mm.Person1"); Object newinstance = Class1.newinstance ();//instanced objectsMethod Setmethod = Class1.getmethod ("Set"+initcap (string), String.class); Method GetMethod = Class1.getmethod ("Get"+initcap (string)); Setmethod.invoke (Newinstance,"Little Over");//equivalent to Newinstance.setname ();System. out. println (Getmethod.invoke (newinstance)); }Private StaticStringInitcap(Stringstring){return string. SUBSTRING (0,1). toUpperCase () +string. SUBSTRING (1); }}
3. Invoke the properties in the class:
Package com.mm;/ * * Reflection * Call properties in class */Import Java.lang.reflect.field;class person3{PrivateString name;} Public classshuxing { Public Static void Main(string[] args) throws exception{class<?> Class1 = Class.forName ("Com.mm.Person3"); Object newinstance = Class1.newinstance ();//instanced objects //This is a property in this class, and if it is inherited, use getfiled ()Field field = Class1.getdeclaredfield ("Name"); Field.setaccessible (true);//Cancel private permissions for attributesField.Set(Newinstance,"Little Over"); StringObject= (String) field.Get(newinstance); System. out. println (Object); }}
Learning experience:
The 1.Class class is the source of the reflection mechanism operation
The objects of the 2.Class class are instantiated in 3 ways;
1) through the GetClass () in the object class
2) through "class."
3) through Class.forName (), most commonly used
3. You can instantiate an object by using the Newinstance () method in class classes, but you must have an parameterless construction method in the class, and you must use the constructor class to complete the instantiation of the object if there are no parameterless constructs in the class
4. The parent class can be implemented by reflection by acquiring a class that inherits the interface of the parent class, all the constructors of the class, all the ordinary square hair, all properties
5. Use the reflection mechanism to invoke methods in a class by method, or to manipulate properties in a class directly
Java Reflection mechanism