(1)註解html
元註解是指註解的註解。包括 @Retention @Target @Document @Inherited四種。java
1.一、@Retention: 定義註解的保留策略api
/*java反射用法*/運行結果以下:
public class TestReflect extends InstrumentationTestCase {
public void testMain() {
LogUtil.e("測試反射:[START]");
try {
main();
} catch (Exception e) {
LogUtil.e("出錯啦:e=" + e.getMessage());
}
LogUtil.e("測試反射:[END]");
}
//★這裏說的Field都是 類 身上的,不是實例上的
public static void main() throws Exception {
Point pt1 = new Point(3, 5);
//獲得一個字段
Field fieldY = pt1.getClass().getField("y"); //y 是變量名
//fieldY的值是5麼?? 大錯特錯
//fieldY和pt1根本沒有什麼關係,你看,是pt1.getClass(),是 字節碼 啊
//不是pt1對象身上的變量,而是類上的,要用它取某個對象上對應的值
//要這樣
LogUtil.e(fieldY.get(pt1).toString()); //這纔是5
//如今要x了
/*
Field fieldX = pt1.getClass().getField("x"); //x 是變量名
LogUtil.e(fieldX.get(pt1));
*/
//運行 報錯 私有的,找不到
//NoSuchFieldException
//說明getField 只能夠獲得 公有的
//怎麼獲得私有的呢??
/*
Field fieldX = pt1.getClass().getDeclaredField("x"); //這個管你公的私的,都拿來
//而後輪到這裏錯了
// java.lang.IllegalAccessException:
//Class com.ncs.ReflectTest can not access a member of class com.ncs.Point with modifiers "private"
LogUtil.e(fieldX.get(pt1));
*/
//三步曲 一是不讓你知道我有錢 二是把錢晃一下,不給用 三是暴力搶了
//暴力反射
Field fieldX = pt1.getClass().getDeclaredField("x"); //這個管你公的私的,都拿來
fieldX.setAccessible(true);//上面的代碼已經看見錢了,開始搶了
LogUtil.e(fieldX.get(pt1).toString());
//out 3 OK!!
}
public static class Point {
private int x;
public int y;
public String s1 = "ball";
public String s2 = "hubin";
public String s3 = "zhangxiaoxiang";
//作實驗而已,字段不多是 public 的
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
}
/*java反射中Method類invoke方法的用法*/運行結果以下:
public class TestReflect extends InstrumentationTestCase {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int add(int param1, int param2) {
return param1 + param2;
}
public String echo(String mesg) {
return "echo" + mesg;
}
public void testMain() {
LogUtil.e("測試反射:[START]");
Class classType = TestReflect.class;
try {
Object invokertester = classType.newInstance(); //1
Method addMethod = classType.getMethod("add", new Class[]{ //2
int.class, int.class
});
Object result = addMethod.invoke(invokertester, new Object[]{ //3
new Integer(100), new Integer(200)
});
LogUtil.e(result.toString());
Method echo = classType.getMethod("echo", new Class[]{String.class});
Object obj = echo.invoke(invokertester,
new Object[]{new String("jy is very good!!!")});
LogUtil.e(obj.toString());
TestReflect test = new TestReflect(); //1
test.setName("小明"); //2
Method[] methods = test.getClass().getDeclaredMethods(); //3
//循環查找獲取id方法,並執行查看是否有返回值
for (int i = 0; i < methods.length; i++) {
//若是此方法有get和Id關鍵字則執行
if (methods[i].getName().indexOf("get") != -1 && methods[i].getName().indexOf("Name") != -1) {
try {
// 獲取此get方法返回值,判斷是否有值,若是沒有值說明即將執行的操做新增
if (methods[i].invoke(test, null) == null) { //4
LogUtil.e("此對象沒有值!!!");
} else {
Object strName = methods[i].invoke(test, null);
LogUtil.e(strName.toString());
}
} catch (Exception e) {
System.out.print("");
}
}
}
} catch (Exception ex) {
LogUtil.e("出錯啦:e=" + ex.getMessage());
}
LogUtil.e("測試反射:[END]");
}
}