1. What is Reflection
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
- Three ways to get a class object
Class name.
The instance object. GetClass ()
Class.forName ("Full path of the class");
Get a constructor object from a class object
Constructor [] cs = clazz.getconstructors (); --Get an array of constructors
Stu Stu = (Stu) clazz.newinstance (); --Call an empty construction method to get the instance object
Constructor C = Clazz.getconstructor (type. Class); --Gets the constructor object by specifying the parameters
Get a Property object from a Class object
Field f = clazz.getfield ("num"); --Get the Public Property object
Field f = clazz.getdeclaredfield ("attribute"); --Get a public or private Property object
- Gets the object of the method through the class object
Method m = Clazz.getmethod ("Method name", parameter type. Class); --Get a common method object
Method m = Clazz.getdeclaredmethod ("Method name", parameter type. Class); --Get the object of a public or private method
Java Reflection Technology