java--Annotation實現

來源:互聯網
上載者:User

標籤:div   col   程式碼範例   text   使用   inter   for   bsp   none   

Annotation實現在java中一共提供了三個annotation:@Override,@Deprecated,@SupperessWarnings代碼範例:使用Annotationpackage com.hbsi.proxy;
@SuppressWarnings("unused")@Deprecatedpublic class RealFood implements Food{ @SuppressWarnings("unused") @Deprecated @Override public void eat() { int x; System.out.println("吃飯 ,,,,,RealFood"); }}

 

如果想要操作Annotation,那麼首先應該可以去的全部的Annotation,如果要想取得Annotation則可以使用Class類中的方法
      • public Annotation[] getAnnotations?() 取得全部的Annotation
程式碼範例:去的上面的類上的Annotation
import java.lang.annotation.Annotation;import com.hbsi.proxy.RealFood;public class TestDemo { public static void main(String[] args) { Class<?> cls = RealFood.class; Annotation[] annotations = cls.getAnnotations(); for (int i = 0; i < annotations.length; i++) { System.out.println(annotations[i]); } }}

 

代碼執行結果:@java.lang.Deprecated(forRemoval=false, since="")奇怪的是這個類上的 @SuppressWarnings("unused")註解並沒有輸出,因為是由Annotation的使用原則決定的,只有@Deprecated才一直支援到運行時使用。其他只是在編譯時間使用如果想知道Annotation 的層級,比如是運行時一直使用,還是只在編譯時間使用,則可以考慮這個類:
  • public enum RetentionPolicy extends Enum<RetentionPolicy> 注釋保留政策。
其枚舉如下
CLASS 注釋將由編譯器記錄在類檔案中,但VM不需要在運行時保留。類之中
RUNTIME 注釋將由編譯器記錄在類檔案中,並由VM在運行時保留,因此可以反射讀取。運行之中
SOURCE 注釋將被編譯器丟棄。原始碼之中
如上,如果現在要編寫一個與程式有關的Annotation,應該使用RUNTIME範圍。 代碼範例:定義一個簡單Annotation
import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;//此註解在運行範圍內有效@Retention(value=RetentionPolicy.RUNTIME)public @interface MyAnnotation { public String name() default "hbsi";//定義name屬性 public String values();//定義values屬性}
而後就可以直接使用此註解: 
import com.hbsi.annotation.MyAnnotation;@MyAnnotation(name="hbsi",values="hebei")public class RealFood implements Food{ @Override public void eat() { int x; System.out.println("吃飯 ,,,,,RealFood"); }}

 再次運行上述代碼中獲得Annotation 的類得到的結果如下:

@com.hbsi.annotation.MyAnnotation(name="hbsi", values="hebei") 代碼範例:
import java.lang.annotation.Annotation;import com.hbsi.annotation.MyAnnotation;import com.hbsi.proxy.RealFood;public class TestDemo { public static void main(String[] args) { Class<?> cls = RealFood.class; Annotation[] annotations = cls.getAnnotations(); for (int i = 0; i < annotations.length; i++) { MyAnnotation annotation = cls.getAnnotation(MyAnnotation.class); System.out.println(annotation.name()); System.out.println(annotation.values()); System.out.println(annotation.annotationType()); } }}

 執行結果

hbsihebeiinterface com.hbsi.annotation.MyAnnotation從執行結果中可以看到,指定類如果配置了此註解並對註解中屬性賦值了,那麼是可以獲得指定類中的為註解賦的值的資訊的。如果要定義有Annotation必然要有一個類專門處理Annotation資料的取得。

java--Annotation實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.