安卓中級教程(7):annotation中的 public @interface的用法

 1 package com.example.ele_me.util;
 2 
 3 import java.lang.annotation.Retention;
 4 import java.lang.annotation.Target;
 5 import java.lang.annotation.ElementType;  
 6 import java.lang.annotation.RetentionPolicy;  
 7 
 8 /** 
 9  * Use this annotation to mark the fields of your Activity as being injectable. 
10  * <p> 
11  * See the {@link Injector} class for more details of how this operates. 
12  */  
13 @Target({ ElementType.FIELD })  
14 @Retention(RetentionPolicy.RUNTIME)  
15 public @interface InjectView {  
16     /** 
17      * 這個java檔最精彩的地方是@interface,一般我們皆看到public Class去定義類型,而在injectview.java這裡卻使用了這樣public @interface的方法。
18      */  
19     public int value();  
20 //所有對象都會被寫進value這個函數之中。
21 }  java.lang.annotation.Retention;

value()所得到的對象內容,會同時被寫進@Target和@Retention之中,當你打開這兩個變數時,你會發現它所包含的東西是如此豐富:

 

1 public enum RetentionPolicy { 
2 SOURCE, 
3 CLASS, 
4 RUNTIME 
5 }

 

1 public enum ElementType { 
2 TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, 
3 LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE 
4 }

 

posted @ 2015-05-18 22:05  少於無  阅读(551)  评论(0编辑  收藏  举报