# Java 文檔注釋
Java只是三種注釋方式。前兩種分別是// 和/\* \*/,第三種被稱作說明注釋,它以/\*\* 開始,以 \*/結束。
說明注釋允許你在程序中嵌入關于程序的信息。你可以使用javadoc工具軟件來生成信息,并輸出到HTML文件中。
說明注釋,是你更加方面的記錄你的程序的信息。
## 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 |
## 文檔注釋
在開始的/\*\*之后,第一行或幾行是關于類、變量和方法的主要描述.
之后,你可以包含一個或多個何種各樣的@標簽。每一個@標簽必須在一個新行的開始或者在一行的開始緊跟星號(\*).
多個相同類型的標簽應該放成一組。例如,如果你有三個@see標簽,可以將它們一個接一個的放在一起。
下面是一個類的說明注釋的示例:
```
/*** This class draws a bar chart.
* @author Zara Ali
* @version 1.2
*/
```
## javadoc輸出什么
javadoc工具將你Java程序的源代碼作為輸入,輸出一些包含你程序注釋的HTML文件。
每一個類的信息將在獨自的HTML文件里。javadoc也可以輸出繼承的樹形結構和索引。
由于javadoc的實現不同,工作也可能不同,你需要檢查你的Java開發系統的版本等細節,選擇合適的Javadoc版本。
### 實例
下面是一個使用說明注釋的簡單實例。注意每一個注釋都在它描述的項目的前面。
在經過javadoc處理之后,SquareNum類的注釋將在SquareNum.html中找到。
```
import java.io.*;
/**
* This class demonstrates documentation comments.
* @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
$
```
- Java 基礎
- Java 簡介
- Java開發環境配置
- Java基礎語法
- Java對象和類
- Java基本數據類型
- Java變量類型
- Java修飾符
- Java運算符
- Java循環結構 - for, while 及 do...while
- Java分支結構 - if...else/switch
- Java Number類
- Java Character類
- Java String類
- Java StringBuffer和StringBuilder類
- Java 數組
- Java 日期時間
- Java正則表達式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 異常處理
- Java 面向對象
- Java 繼承
- Java 重寫(Override)與重載(Overload)
- Java 多態
- Java 抽象類
- Java 接口
- Java 包(package)
- Java 高級教程
- Java 數據結構
- Java Enumeration接口
- Java Bitset類
- Java Vector 類
- Java Stack 類
- Java Dictionary 類
- Java Hashtable 接口
- Java Properties 接口
- Java 集合框架
- Java 泛型
- Java序列化
- Java 網絡編程
- Java 發送郵件
- Java 多線程編程
- Java Applet基礎
- Java 文檔注釋
- Servlet 教程
- Servlet 簡介
- Servlet 環境設置
- Servlet 生命周期
- Servlet 實例
- Servlet 表單數據
- Servlet 客戶端 HTTP 請求
- Servlet 服務器 HTTP 響應
- Servlet HTTP 狀態碼
- Servlet 編寫過濾器
- Servlet 異常處理
- Servlet Cookies 處理
- Servlet Session 跟蹤
- Servlet 數據庫訪問
- Servlet 文件上傳
- Servlet 處理日期
- Servlet 網頁重定向
- Servlet 點擊計數器
- Servlet 自動刷新頁面
- Servlet 發送電子郵件
- Servlet 包
- Servlet 調試
- Servlet 國際化
- JSP 基礎
- JSP 簡介
- JSP 開發環境搭建
- JSP 結構
- JSP 生命周期
- JSP 語法
- JSP 指令
- JSP 動作元素
- JSP 動作元素
- JSP 隱含對象
- JSP 客戶端請求
- JSP 服務器響應
- JSP HTTP 狀態碼
- JSP 表單處理
- JSP 過濾器
- JSP Cookies 處理
- JSP Session
- JSP 文件上傳
- JSP 日期處理
- JSP 頁面重定向
- JSP 點擊量統計
- JSP 自動刷新
- JSP 發送郵件
- JSP 高級教程
- JSP 標準標簽庫(JSTL)
- JSP 連接數據庫
- JSP XML 數據處理
- JSP JavaBean
- JSP 自定義標簽
- JSP 表達式語言
- JSP 異常處理
- JSP 調試
- JSP 國際化
- 免責聲明