天天看點

Java入門 - 進階教程 - 09.文檔注釋文檔注釋

原文位址: http://www.work100.net/training/java-documentation.html 更多教程: 光束雲 - 免費課程

文檔注釋

序号 文内章節 視訊
1 概述 -
2 javadoc标簽
3
4 javadoc輸出什麼

請參照如上

章節導航

進行閱讀

1.概述

Java 支援三種注釋方式。前兩種分别是

//

/* */

,第三種被稱作說明注釋,它以

/**

開始,以

*/

結束。

說明注釋允許你在程式中嵌入關于程式的資訊。你可以使用

javadoc

工具軟體來生成資訊,并輸出到

HTML

檔案中。

說明注釋,使你更加友善的記錄你的程式資訊。

2.javadoc标簽

javadoc

工具軟體識别以下标簽:

标簽 描述 示例

@author

辨別一個類的作者

@author description

@deprecated

指名一個過期的類或成員

@deprecated description

{@docRoot}

指明目前文檔根目錄的路徑

Directory Path

@exception

标志一個類抛出的異常

@exception exception-name explanation

{@inheritDoc}

從直接父類繼承的注釋

Inherits a comment from the immediate surperclass.

{@link}

插入一個到另一個主題的連結

{@link name text}

{@linkplain}

插入一個到另一個主題的連結,但是該連結顯示純文字字型

Inserts an in-line link to another topic.

@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 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

3.文檔注釋

在開始的

/**

之後,第一行或幾行是關于類、變量和方法的主要描述。

之後,你可以包含一個或多個各種各樣的

@

标簽。每一個

@

标簽必須在一個新行的開始或者在一行的開始緊跟星号(

*

).

多個相同類型的标簽應該放成一組。例如,如果你有三個

@see

标簽,可以将它們一個接一個的放在一起。

下面是一個類的說明注釋的執行個體:

/** 
* 這個類繪制一個條形圖
* @author runoob
* @version 1.2
*/           

4.javadoc輸出什麼

javadoc

工具将你 Java 程式的源代碼作為輸入,輸出一些包含你程式注釋的

HTML

檔案。

每一個類的資訊将在獨自的HTML檔案裡。

javadoc

也可以輸出繼承的樹形結構和索引。

由于

javadoc

的實作不同,工作也可能不同,你需要檢查你的 Java 開發系統的版本等細節,選擇合适的

Javadoc

版本。

執行個體

下面是一個使用說明注釋的簡單執行個體。注意每一個注釋都在它描述的項目的前面。

在經過

javadoc

處理之後,

SquareNum

類的注釋将在

SquareNum.html

中找到。

import java.io.*;
 
/**
* 這個類示範了文檔注釋
* @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
$           

上一篇:

Applet
如果對課程内容感興趣,可以掃碼關注我們的

公衆号

QQ群

,及時關注我們的課程更新
Java入門 - 進階教程 - 09.文檔注釋文檔注釋
Java入門 - 進階教程 - 09.文檔注釋文檔注釋