在對(duì)類(lèi)結(jié)構(gòu)進(jìn)行注釋時(shí),傳統(tǒng)的方法是對(duì)每一個(gè)成員變量都要進(jìn)行注釋。例如下面這種情況,非常的麻煩吃警。
public class Generation3List extends Generation2List {
// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy
// class code goes here
}
而一種更簡(jiǎn)單的方式就是定義一個(gè)annotation結(jié)構(gòu)的注釋。
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
// Note use of array
String[] reviewers();
}
這種定義結(jié)構(gòu)和接口的定義方法有些類(lèi)似啄育,這里的關(guān)鍵字@是定義annotation的標(biāo)志酌心。
在定義完annotation后,我們就可以使用它挑豌,具體如下安券。
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
注意:如果想在javadoc中顯示@ClassPreamble的信息,必須在定義ClassPreamble前面加上一句@Documented氓英,并且導(dǎo)入annotation package侯勉,具體如下:
import java.lang.annotation.*;
@Documented
@interface ClassPreamble {
// Annotation element definitions
}