Java中繼資料總結:Java注釋的使用和定義)

來源:互聯網
上載者:User
    中繼資料,就是“關於資料的資料”。Java中繼資料有3種基本類型,還有3個Java內建注釋類型,另外還有4中元注釋類型。本文對其進行介紹與總結。

    中繼資料從metadata一詞譯來,就是“關於資料的資料”的意思。越來越的開源架構都提供了“中繼資料”支援了,其實也就是注釋支援。今天系統學習一下Java注釋(Java中繼資料)。本文內容不限於Javadoc的注釋。

    1.什麼是Java中繼資料,有什麼作用?

    中繼資料,就是“關於資料的資料”。功能也有很多啦。你可能用過Javadoc的注釋自動產生文檔。這就是中繼資料功能的一種。總的來說,中繼資料可以用來建立文檔,跟蹤代碼的依賴性,執行編譯時間格式檢查,代替已有的設定檔(如Hibernate也提供了注釋配置)

    注釋有3中基本類型

    a.標記注釋      --沒有變數,只有名稱標識。例如 @annotation
    b.單一值注釋    --在標記注釋的基礎上提供一段資料。如 @annotation(“data”)
    c.完整注釋      --可以包括多個資料成員,每個資料成員由名稱和值構成。
    @annotation(val1="data1",val2="data2")

    2.Java的“注釋”

    Java中提供3個內建注釋類型

    a. Override ,只能用於方法(不能用於類,包聲明或者其他構造)
    作用:可以保證編譯時間候Override函數的聲明正確性
    用法:@Override
    public void fun(){..}

    b.Deprecated  同樣只能作用與方法
    作用:對不應再使用的方法進行註解
    用法:@Deprecated public void fun{...} //它們說這個注釋跟函數要同一行

    c.SupressWarnings 可以注釋一段代碼
    作用:關閉特定的警告資訊,例如你在使用泛型的時候未指定類型
    用法: @SupressWarnings(value={"unchecked"})
    ..代碼

    Java中還提供了四種元注釋,專門負責注釋其他的注釋

    @Target   表示該注釋可以用於什麼地方。可用的ElementType參數包括:
    CONSTRUCTOR : 構造器的聲明
    FIELD : 域聲明(包括enum執行個體)
    LOCAL_VARIABLE : 局部變數聲明
    METHOD : 方法聲明
    PACKAGE : 包聲明
    PARAMETER : 參數聲明
    TYPE : 類、介面 (包括註解類型) 或enum聲明

    @Retention 表示需要在什麼層級儲存該注釋資訊。可選的RetentionPoicy參數包括:
    SOURCE : 注釋將被編譯器丟掉
    CLASS : 注釋在class檔案中可用,但會被VM丟棄
    RUNTIME : VM將在運行時也保留注釋,因此可以通過反射機制讀取注釋的資訊。

    @Documented 將注釋包含在JavaDoc中

    @Inheried  允許子類繼承父類中的注釋。

    3. 在Java中定義自己的注釋

    Java語言支援一種新的類型——注釋類型(annotation type),跟普通類差不多,在類中以符號( @ )的形式注釋其他 Java 代碼

    下面將通過一個簡單的例子來實現(代碼是Brett McLaughlin 的)
    @interface 申明

    i.簡單的注釋類型

 
  1. package com.oreilly.tiger.ch06;  
  2. /**  
  3.  * Marker annotation to indicate that a method or class  
  4.  * is still in progress.  
  5.  */ 
  6. public @interface InProgress { }  

ii.使用定製的注釋類型

 
  1. @com.oreilly.tiger.ch06.InProgress  
  2. public void calculateInterest(float amout,float rate)  
  3. {  
  4.       //Need to finish this method later  
  5. }  

iii.新增成員

 
  1. package com.oreilly.tiger.ch06;  
  2. /**  
  3.  * Marker annotation to indicate that a method or class  
  4.  * is still in progress.  
  5.  */ 
  6. public @interface InProgress {  
  7.   String value();   
  8.  }  
  9.  
  10. @com.oreilly.tiger.ch06.InProgress  
  11. @TODO("Figure out the amount of interest per month")  
  12. //或者@TODO(value="Figure out the amount of interest per month")  
  13. public void calculateInterest(float amount,float rate)  
  14. {  
  15. }  
  16.  

iv.設定預設值

 
  1. package com.oreilly.tiger.ch06;  
  2. public @interface GroupTODO {  
  3.   public enum Serverity { CRITICAL,IMPORTANT,IRIVIAL,DOCMENTATION };  
  4.   Severity severity()  
  5.           default Severity.IMPORTANT;  
  6.   String item ();  
  7.   String assignedTo();  
  8.   String dateAssigned();  
  9. }  
  10. }  

v.使用預設值

 
  1. @com.oreilly.tiger.ch06.InProgress  
  2. @GroupTODO(  
  3.  item="Figure out the amount of interest per month",  
  4.  assignedTo = "Brett McLaughlin",  
  5.  dateAssigned = "08/04/2004" 
  6. )  
  7.  
  8. public void calculateInterest(float amount, float rate)  
  9. {  
  10.    //Need to finish this method later  
  11. }  
  12.  

vi.改寫預設值

 
  1. @com.oreilly.tiger.ch06.InProgress  
  2. @GroupTODO 
  3. {  
  4.    severity = GroupTODO.Severity.DOCUMENTATION,  
  5.    item = "Need to explain how this rather unusal method works",  
  6.    assignedTo = "Jon Stevens",  
  7.    dateAssigned = "07/30/2004" 

這樣就對Java中繼資料/Java注釋進行了總結。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.