就網上看到的一些例子,個人感覺難點在於註解類定義target的作用範圍與使用該類的方式,再就是使用類給註解賦值
(1)使用target時的位置
target的作用範圍有7個,如下:
1. constructor:用於描述構造器
2. field :用於描述域
3. local_variable 用於描述區域性變數
4. method :用於描述方法
5. package :用於描述包
6. parameter :用於描述引數
7. type :用於描述類、介面(包括註解型別) 或enum宣告。
如果target定義範圍為field,如下:
@target()
@retention(retentionpolicy.runtime)
public @inte***ce column
那麼使用該類的話,只能對欄位使用。
public class aaaaa
如果target定義範圍為type,如下:
@target()
@retention(retentionpolicy.runtime)
public @inte***ce column
那麼使用該類的話,只能對類使用,且在類之前宣告。
@column("aaa")
public class aaaaa
別的幾種情況就不一一舉例了。
(2)使用註解時賦初值
如果只有乙個引數,則預設要用value(),如下
@target()
@retention(retentionpolicy.runtime)
public @inte***ce column
public class filter
"id"可以由下面**獲取
column column = field.getannotation(column.class);
string columnname = column.value();
如果有多個引數,則可以使用自定義的函式名,如下
@target()
@retention(retentionpolicy.runtime)
public @inte***ce column
public class filter
兩個賦值可以由下面**獲取
column column = field.getannotation(column.class);
string columnname1 = column.value1();
string columnname2 = column.value2();
總結:動態獲取的例子目前只是以filed作為例子,還可以以構造器,型別的註解來動態獲取,可以參考
JAVA自定義註解
target elementtype.method retention retentionpolicy.runtime documented public inte ce ignoreauth retention retentionpolicy.source 註解僅存在於原始碼中,在class位元組...
Java 自定義註解Annotation
target elementtype.method retention retentionpolicy.runtime public inte ce operationtype這是乙個比較簡單的自定義註解,target 說明的是註解的作用範圍 1.constructor 用於描述構造器 2.fiel...
自定義View(onMeasure個人理解)
onmeasure方法是view的方法,主要用於測量view大小。onmeasure方法有兩個引數,分別是view寬和高的measurespec。protected void onmeasure int widthmeasurespec,int heightmeasurespec measuresp...