Learn reflection from teacher Wang (a) Java Reflection Overview Teacher: Wang Shaohua QQ Group No.: 483773664 Learning Objectives:
Understanding the Java Reflection mechanism
first, what is Java reflection
Real life, we will see this phenomenon, in a lot of film and television city, will see a lot of ancient buildings, such as the Imperial Palace, but because of the age, the Palace Museum's architectural drawings, lost (we assume that the Palace Museum's drawings lost), so that we must be based on the present National Palace building, drawing its drawings, Then according to the drawing of the drawings, to build a cottage version of the Forbidden City. The process of getting a drawing from a physical object can also be called a "reflection process."
Reflection (reflection) allows the program to obtain internal information of any class with the help of the Reflection API during the execution period, and to manipulate the internal properties and methods of any object directly. The reflection-related APIs are:
Java.lang.Class: Represents a class
Java.lang.reflect.Method: Methods for representing classes
Java.lang.reflect.Field: A member variable representing a class
Java.lang.reflect.Constructor: The construction method of the Representative class
。。。
In fact, in our usual open process, it should have been to the Java reflection mechanism, for example, in MyEclipse, we create an object, and then through the point, the compilation tool will be the object's full properties display
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/89/wKiom1dYCbPz-XiVAABgf2QkBHU244.jpg " data_ue_src= "E:\My knowledge\temp\14180b1e-d2af-46ce-a00f-953b4259409c.jpg" >
This is the use of the Java reflection mechanism.
Two, small case: the charm of the Insight Reflex1. General Category: Floor
1234567891011121314151617181920212223 |
public class Floor {
private String color;
//大楼颜色
int height;
//大楼高度
/**
* 构造方法
*/
public
Floor() {
System.out.println(
"我是Floor的构造方法"
);
}
/**
* 获得color
*/
public void getColor(){
System.out.println(
this
.color);
}
/**
* 大楼的功能
* @param str
*/
public void fun(String str){
System.out.println(str);
}
}
|
2. Test class
1234567891011121314151617181920 |
public static void reflectMethod(Floor floor)
throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException{
//获得图纸:getClass
Class<Floor> floorClass = (Class<Floor>) floor.getClass();
//造楼:newInstance
Floor newFloor = floorClass.newInstance();
//属性赋值: 获得属性
Field field = floorClass.getDeclaredField(
"color"
);
//属性赋值:因为color的属性是private,所以得设置其可访问性
field.setAccessible(
true
);
field.set(newFloor, "白色" );
//to see if the setting is successful
newFloor.getColor();
//获得方法:通过方法名和方法参数类型来区分
Method method = floorClass.getDeclaredMethod(
"fun"
, String.
class
);
//执行方法:
method.invoke(newFloor,
"教学楼"
);
}
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/89/wKiom1dYCbOAkSDNAAAEr4bJCfE518.png " data_ue_src= "E:\My knowledge\temp\20ee8f3e-e98f-4be7-80c3-fd376fd0722f.png" >
From for notes (Wiz)
Learn reflection with teacher Wang (a): Reflection overview