Java的文件注释
以下內(nèi)容引用自http://wiki.jikexueyuan.com/project/java/documentation.html:
Java語(yǔ)言支持三種注釋形式:
| /*text*/ | 編譯器忽略/*到*/的所有東西 |
| //text | 編譯器忽略從//到一行末尾的所有東西 |
| /** documentation */ | 這是文檔注釋并且通常而言它被叫做doc comment。JDK javadoc工具當(dāng)準(zhǔn)備自動(dòng)準(zhǔn)備生成文件時(shí)使用doc comment |
一、什么是Javadoc?
Javadoc是JDK附帶的一個(gè)工具,它被用來(lái)生成從需要預(yù)定義格式的文檔的Java源代碼至HTML格式的Java代碼文件。
以下是一個(gè)簡(jiǎn)單的例子,其中紅色部分代表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 標(biāo)簽包括在描述部分內(nèi),比如,下面的例子利用<h1>...</h1>來(lái)定義頭部和<p>被用來(lái)創(chuàng)建段落間隔:
/** * <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標(biāo)簽
Javadoc標(biāo)簽是Javadoc認(rèn)可的關(guān)鍵字,它定義了下面信息的類型。
Javadoc工具認(rèn)可下面的標(biāo)簽:
| @author | 添加類的作者 | @author name-text | ||
| {@code} | 不把文本轉(zhuǎn)換成HTML標(biāo)記和嵌套的Java標(biāo)簽而用代碼字體展示它 | {@code text} | ||
| {@docRoot} | 表示從任何生成頁(yè)面到生成文檔的根目錄的相對(duì)路徑 | {@docRoot} | ||
| @deprecated | 添加一個(gè)注釋暗示API應(yīng)該不再被使用 | @deprecated deprecated-text | ||
| @exception | 用類名和描述文本給生成的文檔添加一個(gè)副標(biāo)題 | @exception class-name description | ||
| {@inheritDoc} | 從最近的可繼承的類或可實(shí)現(xiàn)的接口繼承注釋 | Inherits a comment from the immediate surperclass. | ||
| {@link} | 用指向特定的包,類或者一個(gè)引用類的成員名的文檔的可見(jiàn)文本標(biāo)簽插入在線鏈接 | {@link package.class#member label} | ||
| {@linkplain} | 和{@link}相同,除了鏈接的標(biāo)簽用純文本標(biāo)示而不是代碼字體 | {@linkplain package.class#member label} | ||
| @param | 給“參數(shù)”區(qū)域添加一個(gè)有特定參數(shù)名且后跟著特定描述的參數(shù) | @param parameter-name description | ||
| @return | 添加一個(gè)有描述文本的“Returns”區(qū)域 | @return description | ||
| @see | 添加帶有鏈接或者指向引用的文本入口的標(biāo)題“See Also” | @see reference | ||
| @serial | 在默認(rèn)的序列化字段的文本注釋中使用 | @serial field-description | include | exclude |
| @serialData | 記錄由writeObject( )或writeExternal( )方法所寫的數(shù)據(jù) | @serialData data-description | ||
| @serialField | 記錄一個(gè)ObjectStreamField成分 | @serialField field-name field-type field-description | ||
| @since | 給生成的文檔添加一個(gè)帶有特定since文本的"Since"標(biāo)題 | @since release | ||
| @throws | @throw和@exception標(biāo)簽是同義詞 | @throws class-name description | ||
| {@value} | 當(dāng){@value}被用在一個(gè)靜態(tài)字段的文本注釋中,它展示了那個(gè)常量的值 | {@value package.class#field} | ||
| @version | 當(dāng)-version選項(xiàng)被使用時(shí)用特定的version文本給生成的文本添加一個(gè)“Version”副標(biāo)題 | @version version-text |
示例:
下面的程序使用一些重要的標(biāo)簽來(lái)做文檔注釋。可以基于需求利用其它的標(biāo)簽。
關(guān)于AddNum類的文檔將由HTML文件AddNum.html創(chuàng)建,但是同時(shí)一個(gè)名為index.html的主文件也將被創(chuàng)建。
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);} }現(xiàn)在,處理使用Javadoc的AddNum.java文件:
$ javadoc AddNum.java Loading source file AddNum.java... Constructing Javadoc information... Standard Doclet version 1.7.0_51 Building 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下載并使用標(biāo)準(zhǔn)的stylesheet。
總結(jié)
- 上一篇: 2017百度之星程序设计大赛 - 资格赛
- 下一篇: 站在智能路由的风口,他选择把传统OA放进