Reflections: Focus----reflect every day, but don't write reflections every day
Java's reflection technology is one of the characteristics of a Java program that allows a running Java program to check itself, or "self-audit," and to manipulate the internal properties of the program directly.
Use reflection to get the names of individual members in the Java class and display them. Simply put, reflection is the technique by which you can get objects (classes, properties, methods) by name.
The effect of reflection
? You can discover the types of objects by reflection mechanism, discover types of methods/Properties/constructors
? You can create objects and access arbitrary object methods and properties, etc.
Primary point: Runtime explores and uses classes that are not known at compile time
Dog D1 = new Dog (); What process does such a class experience when it is created?
In simple terms,
Class loading: Completed by the ClassLoader, a Java.lang.Class object is created when the class file is read into memory. Once a class is loaded into the JVM, the same class is not loaded again.
Connection: Merges the binary data of the class into the JRE.
Initialize: The JVM is responsible for initializing the class, that is, initializing the static property. In a Java class, there are two ways to specify an initial value for a static property:
(1) Specifying the initial value when declaring a static property;
(2) Use static initialization blocks to specify initial values for static properties.
In our case above:
1.JVM uses Dogclassloader to load the dog class into memory and then immediately produces an object of class type, which can be thought of as a model that will be built using the model regardless of how many instances of the dog class are created.
---so there is only one object of class that corresponds to one of the classes.
According to this model, it is the use of Java to make the object's creation and usage by using this kind of loading method.
Using reflection:
Follow these three steps
The first step is to get the Java.lang.Class object of the class you want to manipulate
The second step is to invoke methods such as Getdeclaredmethods
The third step is to use the reflection API to manipulate this information
Simple Case 01:
Class C1 = String.class;
Class C2 = class.forname ("java.lang.String");
Class C3 = "abc". getclass ();
Lab 2: Doing something through reflection
Integer a=13;
Class C=a.getclass ();//Get the type of a
Case 03:
Integer a=13;
Class C=a.getclass ();
Field[] Field=c.getdeclaredfields ();
for (Field F:field) {
System.out.print (F.gettype () + ""); \
System.out.println (F.getname ());
}
We get all the attributes in the integer class
Can you change that?
Dog D=new Dog ();
Class Clazz=d.getclass ();
Field field=clazz.getdeclaredfield ("num");
Field.set (d, 1000);
System.out.println (Field.get (d));
Case 04:
Class C1=outer.class;
Method[] Methods=c1.getdeclaredmethods ();
for (Method method:methods) {
System.out.print (Method.getname () + "");
System.out.print (Method.getreturntype () + "");
System.out.println (Method.getparametertypes ());//If it is an array, what to do with it
}
Get all the methods in the class
Dog D=new Dog ();
Class Clazz=d.getclass ();
Method Method=clazz.getdeclaredmethod ("show", Int.class,string.class);
Method.invoke (d,10, "Zhangsan");
Case 05:
Integer a=13;
Class C=a.getclass ();
Class C1=outer.class;
Constructor [] constructors=c.getdeclaredconstructors ();
for (Constructor constructor:constructors) {
System.out.print (Constructor.getname () + "");
System.out.println (Arrays.tostring (Constructor.getparametertypes ()));
}
Get all the constructors
Focus: Creating instances of classes by reflection
Lab 01:
To create a class instance from a class name
Class Cls=outer.class;//obj refers to object A as a whole.
Object o=cls.newinstance ();
------------------------------------------------------notice it's the same as our instance object?
Class<?> clazz=class.forname ("package name. Class name");
Wode o= (Wode) clazz.newinstance ();
Note the point:
Class.forName () static method, the class name can be used to find the corresponding class in CLASSPATH, and loaded into the internal
Save, return this "class"
Class.forName () The process of loading a class takes "lazy way, that is, check that if it is loaded (in memory) it will not be loaded, directly return the loaded class, equivalent to" manual "to check if a class has been loaded in memory
The. Newinstance () method creates a class instance (instance object) with the default (parameterless) constructor
Add point: Get Class object Three ways
Method One: If an instance of a class has been obtained, you can use the
"Class C = object name. GetClass (); 】
Example: TextField t = new TextField ();
Class C = T.getclass ();
Class s = C.getsuperclass ();
Way two: If you know the name of the class at compile time, you can use the following method
"Class C = jbutton.class; 】
or Class C = integer.type;
If the class name is not known at compile time, but can be obtained at run time, you can use the following method
"Class C = class.forname (STRG); 】
Here to note:
Newinstance: Weak type. Low efficiency. Only parameterless constructs can be called.
NEW: Strongly typed. relatively efficient. Can invoke any public construct.
Newinstance () is an inevitable choice for the implementation of IOC, reflection, interface programming and dependency inversion, and new can only be instantiated for specific classes, not suitable for interface programming.
Manipulating object properties by reflection
Class Clss=outer.class;
Object o=clss.newinstance ();
Field field=clss.getdeclaredfield ("num");
Object Val=field.get (o);
System.out.println (Val);
Above the experiment, we found that in this way is also possible class Cls=outer.class;
--------------------------
And then the experiment above, we found the
Object Val=field.get (o);
val=15;
Object Val2=field.get (o);
System.out.println (Val);
In this way, we can completely modify our properties
2016/05/04 (Reflection)