Java 支持三種注釋方式。前兩種分別是 // 和 /* */,第三種被稱作說明注釋,它以 /** 開始,以 */結(jié)束。
說明注釋允許你在程序中嵌入關(guān)于程序的信息。你可以使用 javadoc 工具軟件來生成信息,并輸出到HTML文件中。
說明注釋,使你更加方便的記錄你的程序信息。
javadoc 工具軟件識(shí)別以下標(biāo)簽:
標(biāo)簽 | 描述 | 示例 |
---|---|---|
@author | 標(biāo)識(shí)一個(gè)類的作者 | @author description |
@deprecated | 指名一個(gè)過期的類或成員 | @deprecated description |
{@docRoot} | 指明當(dāng)前文檔根目錄的路徑 | Directory Path |
@exception | 標(biāo)志一個(gè)類拋出的異常 | @exception exception-name explanation |
{@inheritDoc} | 從直接父類繼承的注釋 | Inherits a comment from the immediate surperclass. |
{@link} | 插入一個(gè)到另一個(gè)主題的鏈接 | {@link name text} |
{@linkplain} | 插入一個(gè)到另一個(gè)主題的鏈接,但是該鏈接顯示純文本字體 | Inserts an in-line link to another topic. |
@param | 說明一個(gè)方法的參數(shù) | @param parameter-name explanation |
@return | 說明返回值類型 | @return explanation |
@see | 指定一個(gè)到另一個(gè)主題的鏈接 | @see anchor |
@serial | 說明一個(gè)序列化屬性 | @serial description |
@serialData | 說明通過writeObject( ) 和 writeExternal( )方法寫的數(shù)據(jù) | @serialData description |
@serialField | 說明一個(gè)ObjectStreamField組件 | @serialField name type description |
@since | 標(biāo)記當(dāng)引入一個(gè)特定的變化時(shí) | @since release |
@throws | 和 @exception標(biāo)簽一樣. | The @throws tag has the same meaning as the @exception tag. |
{@value} | 顯示常量的值,該常量必須是static屬性。 | Displays the value of a constant, which must be a static field. |
@version | 指定類的版本 | @version info |
在開始的 /** 之后,第一行或幾行是關(guān)于類、變量和方法的主要描述。
之后,你可以包含一個(gè)或多個(gè)各種各樣的 @ 標(biāo)簽。每一個(gè) @ 標(biāo)簽必須在一個(gè)新行的開始或者在一行的開始緊跟星號(hào)(*).
多個(gè)相同類型的標(biāo)簽應(yīng)該放成一組。例如,如果你有三個(gè) @see 標(biāo)簽,可以將它們一個(gè)接一個(gè)的放在一起。
下面是一個(gè)類的說明注釋的示例:
/*** 這個(gè)類繪制一個(gè)條形圖 * @author nhooo * @version 1.2 */
javadoc 工具將你 Java 程序的源代碼作為輸入,輸出一些包含你程序注釋的HTML文件。
每一個(gè)類的信息將在獨(dú)自的HTML文件里。javadoc 也可以輸出繼承的樹形結(jié)構(gòu)和索引。
由于 javadoc 的實(shí)現(xiàn)不同,工作也可能不同,你需要檢查你的 Java 開發(fā)系統(tǒng)的版本等細(xì)節(jié),選擇合適的 Javadoc 版本。
下面是一個(gè)使用說明注釋的簡單示例。注意每一個(gè)注釋都在它描述的項(xiàng)目的前面。
在經(jīng)過 javadoc 處理之后,SquareNum 類的注釋將在 SquareNum.html 中找到。
import java.io.*; /** * 這個(gè)類演示了文檔注釋 * @author Ayan Amhed * @version 1.2 */ public class SquareNum { /** * This method returns the square of num. * This is a multiline description. You can use * as many lines as you like. * @param num The value to be squared. * @return num squared. */ public double square(double num) { return num * num; } /** * This method inputs a number from the user. * @return The value input as a double. * @exception IOException On input error. * @see IOException */ public double getNumber() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader inData = new BufferedReader(isr); String str; str = inData.readLine(); return (new Double(str)).doubleValue(); } /** * This method demonstrates square(). * @param args Unused. * @return Nothing. * @exception IOException On input error. * @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 文件:
$ 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 $