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
}
使用注解添加相同的元数据,首先你需要定义注解类型。语法如下所示:
@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();
}
注解类型的定义看起来和接口的定义很想,最是在interface关键词前加了at标志(@) (@ = AT, as in annotation type)。注解类型是接口的一种形式,在后面的章节中我们会介绍。
上面注解的定义中包含annotation type element的声明,看起来像函数。注意它们可以可选定义默认值。
当注解已经定义,你可以使用它,记得填写值,例子如下:
@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
}