Java的檔案注釋

來源:互聯網
上載者:User

標籤:roo   使用   ant   source   package   工具   throw   序列   code   

以下內容引用自http://wiki.jikexueyuan.com/project/java/documentation.html:

Java語言支援三種注釋形式:

注釋 描述
/*text*/ 編譯器忽略/*到*/的所有東西
//text 編譯器忽略從//到一行末尾的所有東西
/**
documentation
*/
這是文檔注釋並且通常而言它被叫做doc comment。JDK javadoc工具當準備自動準備組建檔案時使用doc comment

一、什麼是Javadoc?

Javadoc是JDK附帶的一個工具,它被用來產生從需要預定義格式的文檔的Java原始碼至HTML格式的Java代碼檔案。

以下是一個簡單的例子,其中紅色部分代表Java注釋:

/*** The HelloWorld program implements an application that* simply displays "Hello World!" to the standard output.** @author  Zara Ali* @version 1.0* @since   2014-03-31 */public class HelloWorld {    public static void main(String[] args) {        // Prints Hello, World! on standard output.        System.out.println("Hello World!");    }}

可以將需要的HTM 標籤包括在描述部分內,比如,下面的例子利用<h1>...</h1>來定義頭部和<p>被用來建立段落間隔:

/*** <h1>Hello, World!</h1>* The HelloWorld program implements an application that* simply displays "Hello World!" to the standard output.* <p>* Giving proper comments in your program makes it more* user friendly and it is assumed as a high quality code.* ** @author  Zara Ali* @version 1.0* @since   2014-03-31 */public class HelloWorld {    public static void main(String[] args) {        // Prints Hello, World! on standard output.        System.out.println("Hello World!");    }}

二、Javadoc標籤

Javadoc標籤是Javadoc認可的關鍵字,它定義了下面資訊的類型。

Javadoc工具認可下面的標籤:

標籤 描述 文法
@author 添加類的作者 @author name-text
{@code} 不把文本轉換成HTML標記和嵌套的Java標籤而用代碼字型展示它 {@code text}
{@docRoot} 表示從任何產生頁面到產生文檔的根目錄的相對路徑 {@docRoot}
@deprecated 添加一個注釋暗示API應該不再被使用 @deprecated deprecated-text
@exception 用類名和描述文本給產生的文檔添加一個副標題 @exception class-name description
{@inheritDoc} 從最近的可繼承的類或可實現的介面繼承注釋 Inherits a comment from the immediate surperclass.
{@link} 用指向特定的包,類或者一個引用類的成員名的文檔的可見文字標籤插入線上連結 {@link package.class#member label}
{@linkplain} 和{@link}相同,除了連結的標籤用純文字標示而不是代碼字型 {@linkplain package.class#member label}
@param 給“參數”地區添加一個有特定參數名且後跟著特定描述的參數 @param parameter-name description
@return 添加一個有描述文本的“Returns”地區 @return description
@see 添加帶有連結或者指向引用的文本入口的標題“See Also” @see reference
@serial 在預設的序列化欄位的文本注釋中使用 @serial field-description include exclude
@serialData 記錄由writeObject( )或writeExternal( )方法所寫的資料 @serialData data-description
@serialField 記錄一個ObjectStreamField成分 @serialField field-name field-type field-description
@since 給產生的文檔添加一個帶有特定since文本的"Since"標題 @since release
@throws @throw和@exception標籤是同義字 @throws class-name description
{@value} 當{@value}被用在一個靜態欄位的文本注釋中,它展示了那個常量的值 {@value package.class#field}
@version 當-version選項被使用時用特定的version文本給產生的文本添加一個“Version”副標題 @version version-text

樣本:

下面的程式使用一些重要的標籤來做文檔注釋。可以基於需求利用其它的標籤。

關於AddNum類的文檔將由HTML檔案AddNum.html建立,但是同時一個名為index.html的主檔案也將被建立。

import java.io.*;/*** <h1>Add Two Numbers!</h1>* The AddNum program implements an application that* simply adds two given integer numbers and Prints* the output on the screen.* <p>* <b>Note:</b> Giving proper comments in your program makes it more* user friendly and it is assumed as a high quality code.** @author  Zara Ali* @version 1.0* @since   2014-03-31*/public class AddNum {   /**   * This method is used to add two integers. This is   * a the simplest form of a class method, just to   * show the usage of various javadoc Tags.   * @param numA This is the first paramter to addNum method   * @param numB  This is the second parameter to addNum method   * @return int This returns sum of numA and numB.   */   public int addNum(int numA, int numB) {      return numA + numB;   }   /**   * This is the main method which makes use of addNum method.   * @param args Unused.   * @return Nothing.   * @exception IOException On input error.   * @see IOException   */   public static void main(String args[]) throws IOException   {      AddNum obj = new AddNum();      int sum = obj.addNum(10, 20);      System.out.println("Sum of 10 and 20 is :" + sum);   }}

現在,處理使用Javadoc的AddNum.java檔案:

$ javadoc AddNum.javaLoading source file AddNum.java...Constructing Javadoc information...Standard Doclet version 1.7.0_51Building tree for all the packages and classes...Generating /AddNum.html...AddNum.java:36: 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...1 warning$

如果正在使用JDK 1.7那麼Javadoc不產生stysheet.css,所以建議從http://docs.oracle.com/javase/7/docs/api/stylesheet.css下載並使用標準的stylesheet。

Java的檔案注釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.