自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java元數(shù)據(jù)總結(jié):Java注釋的使用和定義

開發(fā) 后端
元數(shù)據(jù),就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)”。Java元數(shù)據(jù)有3種基本類型,還有3個Java內(nèi)置注釋類型,另外還有4中元注釋類型。本文對其進行介紹與總結(jié)。

元數(shù)據(jù)從metadata一詞譯來,就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)”的意思。越來越的開源框架都提供了“元數(shù)據(jù)”支持了,其實也就是注釋支持。今天系統(tǒng)學(xué)習(xí)一下Java注釋(Java元數(shù)據(jù))。本文內(nèi)容不限于Javadoc的注釋。

1.什么是Java元數(shù)據(jù),有什么作用?

元數(shù)據(jù),就是“關(guān)于數(shù)據(jù)的數(shù)據(jù)”。功能也有很多啦。你可能用過Javadoc的注釋自動生成文檔。這就是元數(shù)據(jù)功能的一種??偟膩碚f,元數(shù)據(jù)可以用來創(chuàng)建文檔,跟蹤代碼的依賴性,執(zhí)行編譯時格式檢查,代替已有的配置文件(如Hibernate也提供了注釋配置)

注釋有3中基本類型

a.標(biāo)記注釋      --沒有變量,只有名稱標(biāo)識。例如 @annotation
b.單一值注釋    --在標(biāo)記注釋的基礎(chǔ)上提供一段數(shù)據(jù)。如 @annotation(“data”)
c.完整注釋      --可以包括多個數(shù)據(jù)成員,每個數(shù)據(jù)成員由名稱和值構(gòu)成。
                  @annotation(val1="data1",val2="data2")

2.Java的“注釋”

Java中提供3個內(nèi)置注釋類型

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

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

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

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

@Target   表示該注釋可以用于什么地方??捎玫腅lementType參數(shù)包括:
          CONSTRUCTOR : 構(gòu)造器的聲明
          FIELD : 域聲明(包括enum實例)
          LOCAL_VARIABLE : 局部變量聲明
          METHOD : 方法聲明
          PACKAGE : 包聲明
          PARAMETER : 參數(shù)聲明
          TYPE : 類、接口 (包括注解類型) 或enum聲明

@Retention 表示需要在什么級別保存該注釋信息??蛇x的RetentionPoicy參數(shù)包括:
           SOURCE : 注釋將被編譯器丟掉
           CLASS : 注釋在class文件中可用,但會被VM丟棄
           RUNTIME : VM將在運行時也保留注釋,因此可以通過反射機制讀取注釋的信息。

@Documented 將注釋包含在JavaDoc中

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

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

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

下面將通過一個簡單的例子來實現(xiàn)(代碼是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.設(shè)置默認值

  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元數(shù)據(jù)/Java注釋進行了總結(jié)。

【編輯推薦】

  1. J2SE5.0中的注釋特性
  2. JavaDoc注釋的使用
  3. EJB注釋是通過@來實現(xiàn)的
  4. Eclipse+JBoss+EJB3的Session Bean注釋方法
  5. J2SE1.5注釋語法簡介
責(zé)任編輯:yangsai 來源: JavaEye博客
相關(guān)推薦

2023-11-14 10:05:52

Java開發(fā)工具

2015-03-09 14:18:41

Java注釋原則

2021-09-01 10:37:25

鴻蒙HarmonyOS應(yīng)用

2021-12-30 12:30:01

Java注解編譯器

2009-06-16 11:29:32

Javadoc

2012-03-15 11:21:19

Java

2010-06-11 17:10:02

Java框架開源

2010-01-08 14:36:01

生成 JSON

2022-06-09 21:19:21

元宇宙多維度互聯(lián)網(wǎng)

2023-08-07 09:00:00

2009-07-29 17:42:55

JavaScript和JavaJScript

2009-12-21 16:37:41

WCF獲取服務(wù)元數(shù)據(jù)

2020-02-07 08:00:00

ExifTool提取文件開源

2016-12-15 13:31:20

Java異常處理經(jīng)驗

2023-05-08 08:00:00

2022-06-24 09:58:35

大數(shù)據(jù)JavaPython

2012-02-08 09:44:52

Java反射

2023-03-13 10:01:27

Java注釋

2023-10-30 15:28:51

數(shù)據(jù)倉庫

2021-09-27 23:58:55

數(shù)據(jù)庫分層設(shè)計
點贊
收藏

51CTO技術(shù)棧公眾號