1. Reflections.
Class: Reflection class Any one class has a class reflector. Shadow
java.lang.reflect.*;
Field: Fields Class
Method: Methods Class class
Constructor: Constructs a method class.
2. Get the Reflection class
1. Get the Reflection class class name. class
Class Clazz1=people.class;
2. Through the full class name
Class Clazz2=class.forname ("demo. People ");
3. Object acquisition
People p=new people ();
Class Clazz3=p.getclass ();
3. Instantiate the corresponding class object by reflection class.
Clazz.newinstance ();
4. Gets the properties in the class through the reflection class.
Gets the properties of the parent class or the public adornment in this class
Field agefield= Clazz1.getfield ("Age");
Agefield.set (P, 156);
Get only the properties that you declare in this class
Field Namefield=clazz1.getdeclaredfield ("name");
Namefield.setaccessible (TRUE);//Allow assignment
Namefield.set (P, "Zhang San");
SYSTEM.OUT.PRINTLN (P);
5. Get all the methods of a class
Method showmethod= Clazz1.getmethod ("print", int.class);
Call this method
Showmethod.invoke (p,15);
Method[] Ms=clazz1.getdeclaredmethods ();
for (Method m:ms) {
System.out.println (Modifier.tostring (M.getmodifiers ()) + ""
+m.getreturntype (). GetName () + "" +m.getname () + "(" +arrays.aslist (M.getgenericparametertypes ()) + ")");
}
Java Basic Note 14