Java 提供三種註解://、/* */ 以及文檔註解(以 /** 開頭、*/ 結束)。文檔註解允許在程式碼中嵌入程式資訊,透過 javadoc 工具產出 HTML 文檔。
| 標籤 | 描述 | 範例 |
|---|---|---|
@author | 類別作者 | @author description |
@deprecated | 標記過期類別或成員 | @deprecated description |
{@docRoot} | 當前文檔根目錄路徑 | Directory Path |
@exception | 類別拋出的異常 | @exception exception-name explanation |
{@inheritDoc} | 繼承自父類的註解 | Inherits a comment... |
{@link} | 插入連結到其他主題 | {@link name text} |
{@linkplain} | 插入連結(純文字字型) | Inserts an in-line link... |
@param | 方法參數說明 | @param parameter-name explanation |
@return | 回傳值說明 | @return explanation |
@see | 參考連結 | @see anchor |
@serial | 序列化屬性說明 | @serial description |
@serialData | writeObject/writeExternal 寫入資料 | @serialData description |
@serialField | ObjectStreamField 元件 | @serialField name type description |
@since | 引入變更的版本 | @since release |
@throws | 同 @exception | The @throws tag... |
{@value} | 顯示常數值(需為 static) | Displays the value... |
@version | 類別版本 | @version info |
/** 開頭,首行為類別/變數/方法的主要描述@ 標籤,每個標籤佔一行(或緊接 * 開頭)@see 連續排列)/** * 這個類別繪製長條圖 * @author runoob * @version 1.2 */
import java.io.*;
/**
* 這個類別示範文檔註解
* @author Ayan Amhed
* @version 1.2
*/
public class SquareNum {
/**
* 回傳 num 的平方
* @param num 要平方的值
* @return num 的平方
*/
public double square(double num) {
return num * num;
}
/**
* 從使用者輸入取得數字
* @return 輸入的 double 值
* @exception IOException 輸入錯誤
* @see IOException
*/
public double getNumber() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader inData = new BufferedReader(isr);
String str = inData.readLine();
return (new Double(str)).doubleValue();
}
/**
* 示範 square() 方法
* @param args 未使用
* @return 無
* @exception IOException 輸入錯誤
* @see IOException
*/
public static void main(String args[]) throws IOException {
SquareNum ob = new SquareNum();
double val;
System.out.println("Enter value to be squared: ");
val = ob.getNumber();
val = ob.square(val);
System.out.println("Squared value is " + val);
}
}
$ javadoc SquareNum.java Loading source file SquareNum.java... Constructing Javadoc information... Standard Doclet version 1.5.0_13 Building tree for all the packages and classes... Generating SquareNum.html... SquareNum.java:39: warning - @return tag cannot be used in method with void return type. Generating package-frame.html... Generating package-summary.html... Generating package-tree.html... Generating constant-values.html... Building index for all the packages and classes... Generating overview-tree.html... Generating index-all.html... Generating deprecated-list.html... Building index for all classes... Generating allclasses-frame.html... Generating allclasses-noframe.html... Generating index.html... Generating help-doc.html... Generating stylesheet.css... 1 warning $
{@link} 連結相關類別與方法