如何写Java文档注释(Java Doc Comments)
本文翻譯自How to Write Doc Comments for the Javadoc Tool,但是精簡(jiǎn)了一些私以為不重要的東西
本文不討論如何使用javadoc工具自動(dòng)生成文檔的方法,而是主要探討應(yīng)該如何去寫(xiě)文檔注釋
業(yè)余時(shí)間整理,難免有遺漏或錯(cuò)誤,如有發(fā)現(xiàn)歡迎指正
轉(zhuǎn)載請(qǐng)注明
?
文檔注釋概覽
“文檔注釋”(Java Doc Comments)是專門(mén)為了用javadoc工具自動(dòng)生成文檔而寫(xiě)的注釋,它是一種帶有特殊功能的注釋。
文檔注釋與一般注釋的最大區(qū)別在于起始符號(hào)是/**而不是/*或//。
比如:
/** * 這是文檔注釋 *//* * 這是一般注釋 */// 這是一般注釋在一些IDE(比如Eclipse)中,文檔注釋會(huì)以不同于普通注釋的顏色高亮顯示。
此外,文檔注釋只負(fù)責(zé)描述類(class)、接口(interface)、方法(method)、構(gòu)造器(constructor)、成員字段(field)。相應(yīng)地,文檔注釋必須寫(xiě)在類、接口、方法、構(gòu)造器、成員字段前面,而寫(xiě)在其他位置,比如函數(shù)內(nèi)部,是無(wú)效的文檔注釋。
文檔注釋采用HTML語(yǔ)法規(guī)則書(shū)寫(xiě),支持HTML標(biāo)記(tag),同時(shí)也有一些額外的輔助標(biāo)記。需要注意的是,這些標(biāo)記不是給人看的(通常他們的可讀性也不好),他們的作用是為了javadoc工具更好地生成最終文檔。所以,雖然有些標(biāo)記寫(xiě)起來(lái)麻煩且看著不直觀,還是要老老實(shí)實(shí)按規(guī)矩寫(xiě)滴。
?
文檔注釋的基本內(nèi)容
一個(gè)文檔注釋由兩部分組成:
/** * 描述部分(description) * * 標(biāo)記部分(block tags) */描述部分自然不用多說(shuō),所謂的標(biāo)記符號(hào)指的是@param, @return, @see之類的,相信只要看過(guò)開(kāi)源java代碼的話應(yīng)該都見(jiàn)過(guò)。
?
下面是一個(gè)描述一個(gè)方法的文檔注釋的例子
/*** Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute {@link URL}. The name* argument is a specifier that is relative to the url argument. * <p>* This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on* the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. ** @param url an absolute URL giving the base location of the image* @param name the location of the image, relative to the url argument* @return the image at the specified URL* @see Image*/ public Image getImage(URL url, String name) {try {return getImage(new URL(url, name));} catch (MalformedURLException e) {return null;} }?
需要注意的幾點(diǎn):
1.?第一行以特殊的文檔定界符?/**?開(kāi)頭
3.?在描述段落和標(biāo)記段落之間空一行,描述段落和標(biāo)記段落必須分開(kāi),不能揉在一起,描述段落必須在標(biāo)記段落之前
4. 每一行注釋都應(yīng)該跟后面描述的類、方法等保持同樣距離的縮進(jìn),比如這樣就是錯(cuò)誤的
5. 從javadoc 1.4之后,除第一行和最后一行外,可以省略其他行的前導(dǎo)星號(hào)(*),但是一般不這么做
?
描述部分(Description)
描述部分的第一行應(yīng)該是一句對(duì)類、接口、方法等的簡(jiǎn)單描述,這句話最后會(huì)被javadoc工具提取并放在索引目錄中。
怎么界定第一句話到哪結(jié)束了呢?答案是跟在第一個(gè)句號(hào)(英文標(biāo)點(diǎn))之后的tab、空行或行終結(jié)符規(guī)定了第一句的結(jié)尾。
例如下面這句注釋,第一句的結(jié)尾是Prof.:
/** * This is a simulation of Prof. Knuth's MIX computer. */?
除了普通的文本之外,描述部分可以使用:
1. HTML語(yǔ)法標(biāo)簽,例如?<b>xxx</b>?
2. javadoc規(guī)定的特殊標(biāo)簽,例如?{@link xxx}?。標(biāo)簽的語(yǔ)法規(guī)則是:{@標(biāo)簽名 標(biāo)簽內(nèi)容}
需要注意的地方:
1. 標(biāo)簽在有javadoc工具生成文檔時(shí)會(huì)轉(zhuǎn)化成特殊的內(nèi)容,比如?{@link URL}?標(biāo)簽會(huì)被轉(zhuǎn)化成指向URL類的超鏈接
2. 如果注釋包含多段內(nèi)容,段與段之間需要用?<p>?分隔,空行是沒(méi)用的
3. 最后結(jié)尾行?*/?和起始行不同,這里只有一個(gè)星號(hào)
4. 為了避免一行過(guò)長(zhǎng)影響閱讀效果,務(wù)必將每行的長(zhǎng)度限制在80個(gè)字符以內(nèi)
5. 善用javadoc工具的復(fù)制機(jī)制避免不必要的注釋:
如果一個(gè)方法覆蓋了父類的方法或?qū)崿F(xiàn)了接口種的方法,那么javadoc工具會(huì)在該注釋里添加指向原始方法的鏈接,此外如果新方法沒(méi)有注釋,那么javadoc會(huì)把原始方法的注釋復(fù)制一份作為其注釋,但是如果新方法有注釋了,就不會(huì)復(fù)制了。?
注釋風(fēng)格:
1. 使用?<code>關(guān)鍵字</code>?來(lái)強(qiáng)調(diào)關(guān)鍵字,建議強(qiáng)調(diào)的內(nèi)容有:java關(guān)鍵字、包名、類名、方法名、接口名、字段名、參數(shù)名等
2. 控制?{@link xxx}?的數(shù)量,太多的鏈接會(huì)使文檔的可讀性很差,因?yàn)樽x者總是跳來(lái)跳去。不要出現(xiàn)相同的鏈接,同樣的鏈接只保留第一個(gè);不要為java自帶的內(nèi)容或是常識(shí)性的內(nèi)容提供鏈接
3. 描述一個(gè)方法時(shí),應(yīng)當(dāng)只保留方法名字,不要附帶方法的參數(shù)。比如有個(gè)方法是add(Object obj),那么用add指代該方法即可,而不是add(Object obj)
4. 英文注釋可以是短語(yǔ)也可以是句子。如果是句子,首字母要大寫(xiě),如果是短語(yǔ),首字母小寫(xiě)。
5. 英文注釋使用第三人稱,而不是第二人稱。比如:
?
6. 方法的注釋?xiě)?yīng)該以動(dòng)詞或動(dòng)詞詞組開(kāi)頭,因?yàn)榉椒ㄊ且粋€(gè)動(dòng)作。比如:
/** * Gets the label of this button(建議) *//** * This method gets the label(不建議) */?
7. 當(dāng)描述類、接口、方法這類的概念時(shí),可以不用指名"類"、"接口"、"方法"這些詞語(yǔ),比如:
/** * A button label (建議) *//** * This field is a button label (不建議) */?
8. 英文使用this而不是the指代當(dāng)前類,比如:
/** * Gets the toolkit for this component (建議) *//** * Gets the toolkit for the component (不建議) */?
9. API名應(yīng)該是能夠簡(jiǎn)單自我說(shuō)明的,如果文檔注釋只是簡(jiǎn)單重復(fù)API的名稱還不如沒(méi)有文檔,所以文檔注釋?xiě)?yīng)該至少提供一些額外信息,否則干脆不要注釋
10. 英文注釋避免拉丁風(fēng)格的縮寫(xiě)。比如使用"also knwon as"而不是"aka", 使用"that is"或"to be specific"而不是"i.e.",使用"for example"而不是"e.g.",使用"in other words"或"namely"而不是"viz."
?
標(biāo)記部分(Tag)
標(biāo)記部分跟在描述部分之后,且前面必須有一個(gè)空行間隔
常見(jiàn)標(biāo)記:
1. ?@author??作者,沒(méi)有特殊格式要求,名字或組織名稱都可以
2. ?@version??軟件版本號(hào)(注意不是java版本號(hào)),沒(méi)有特殊格式要求
3. ?@param? 方法參數(shù),格式為:?@param 參數(shù)名稱 參數(shù)描述?
- 可以在參數(shù)描述中說(shuō)明參數(shù)的類型
- 可以在參數(shù)名稱和參數(shù)描述之間添加額外的空格來(lái)對(duì)齊
- 破折號(hào)或其他標(biāo)點(diǎn)符號(hào)不能出現(xiàn)在參數(shù)描述之外的地方
4. ?@return? 方法返回值,格式為:?@return?返回值描述?,如果方法沒(méi)有返回值就不要寫(xiě)@return
5. ?@deprecated?應(yīng)該告訴用戶這個(gè)API被哪個(gè)新方法替代了,隨后用?@see?標(biāo)記或?{@link}?標(biāo)記指向新API,比如:
6. ?@throws?(或?@exception?)包含方法顯式拋出的檢查異常(Checked Exception),至于非顯示拋出的其他異常(Unchecked Exception),除非特別有必要,否則就別寫(xiě)了。一個(gè)原則就是,只記錄可控的問(wèn)題,對(duì)于不可控的或不可預(yù)測(cè)的問(wèn)題,不要往上面寫(xiě)。
檢查異常:在try語(yǔ)法塊中觸發(fā),在catch塊中捕獲的異常,這些異常會(huì)由編譯器在編譯階段檢查并強(qiáng)制程序員處理非檢查異常:包括運(yùn)行時(shí)異常(RuntimeException)和錯(cuò)誤(Error)。
?
7. 自定義標(biāo)記
?
注釋風(fēng)格:
1. 按照如下順序提供標(biāo)記
此外,如果有多個(gè)相同標(biāo)記,也要注意順序:
多個(gè)@author標(biāo)記,應(yīng)該按照時(shí)間順序排列,即原作者應(yīng)該排在第一個(gè)位置 多個(gè)@param標(biāo)記,應(yīng)該按照參數(shù)定義的順序排列 多個(gè)@exception(或是@thrown)應(yīng)該按照異常的字母順序排列 多個(gè)@see標(biāo)記,應(yīng)該按照注釋的邏輯順序排列,即從最近的到最遠(yuǎn)的,從最具體的到最一般的2. 必須包含的標(biāo)記
如果方法有參數(shù),@param標(biāo)記必須包含,而且每個(gè)對(duì)應(yīng)一個(gè)參數(shù) 如果方法有返回值,@return標(biāo)記必須包含?
?
其他注釋
1. 包級(jí)別的文檔注釋
從java1.2起允許包級(jí)別的文檔注釋,用以描述包信息。每個(gè)包都可以有自己的包文檔注釋,這些注釋被寫(xiě)在叫package.html的單獨(dú)文件中,并且放至于與源碼(*.java)相同的路徑下,注意,一定不能單獨(dú)放置在其他路徑。
javadoc工具按照以下流程處理package.html:
?
在包注釋主要介紹一下這個(gè)包大致是做什么用的、背景信息、在使用方面需要注意的地方等等信息
2. 匿名、內(nèi)部類的文檔注釋
javadoc不會(huì)提取內(nèi)部類的文檔注釋,所以如果想要在最終生成的文檔中包含內(nèi)部類的信息,方法就是——寫(xiě)在外部類的文檔注釋里。。
?
一個(gè)復(fù)雜的文檔注釋例子
/*** Graphics is the abstract base class for all graphics contexts* which allow an application to draw onto components realized on* various devices or onto off-screen images.* A Graphics object encapsulates the state information needed* for the various rendering operations that Java supports. This* state information includes:* <ul>* <li>The Component to draw on* <li>A translation origin for rendering and clipping coordinates* <li>The current clip* <li>The current color* <li>The current font* <li>The current logical pixel operation function (XOR or Paint)* <li>The current XOR alternation color* (see <a href="#setXORMode">setXORMode</a>)* </ul>* <p>* Coordinates are infinitely thin and lie between the pixels of the* output device.* Operations which draw the outline of a figure operate by traversing* along the infinitely thin path with a pixel-sized pen that hangs* down and to the right of the anchor point on the path.* Operations which fill a figure operate by filling the interior* of the infinitely thin path.* Operations which render horizontal text render the ascending* portion of the characters entirely above the baseline coordinate.* <p>* Some important points to consider are that drawing a figure that* covers a given rectangle will occupy one extra row of pixels on* the right and bottom edges compared to filling a figure that is* bounded by that same rectangle.* Also, drawing a horizontal line along the same y coordinate as* the baseline of a line of text will draw the line entirely below* the text except for any descenders.* Both of these properties are due to the pen hanging down and to* the right from the path that it traverses.* <p>* All coordinates which appear as arguments to the methods of this* Graphics object are considered relative to the translation origin* of this Graphics object prior to the invocation of the method.* All rendering operations modify only pixels which lie within the* area bounded by both the current clip of the graphics context* and the extents of the Component used to create the Graphics object.* * @author Sami Shaio* @author Arthur van Hoff* @version %I%, %G%* @since 1.0*/ public abstract class Graphics {/** * Draws as much of the specified image as is currently available* with its northwest corner at the specified coordinate (x, y).* This method will return immediately in all cases, even if the* entire image has not yet been scaled, dithered and converted* for the current output device.* <p>* If the current output representation is not yet complete then* the method will return false and the indicated * {@link ImageObserver} object will be notified as the* conversion process progresses.** @param img the image to be drawn* @param x the x-coordinate of the northwest corner* of the destination rectangle in pixels* @param y the y-coordinate of the northwest corner* of the destination rectangle in pixels* @param observer the image observer to be notified as more* of the image is converted. May be * <code>null</code>* @return <code>true</code> if the image is completely * loaded and was painted successfully; * <code>false</code> otherwise.* @see Image* @see ImageObserver* @since 1.0*/public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);/*** Dispose of the system resources used by this graphics context.* The Graphics context cannot be used after being disposed of.* While the finalization process of the garbage collector will* also dispose of the same system resources, due to the number* of Graphics objects that can be created in short time frames* it is preferable to manually free the associated resources* using this method rather than to rely on a finalization* process which may not happen for a long period of time.* <p>* Graphics objects which are provided as arguments to the paint* and update methods of Components are automatically disposed* by the system when those methods return. Programmers should,* for efficiency, call the dispose method when finished using* a Graphics object only if it was created directly from a* Component or another Graphics object.** @see #create(int, int, int, int)* @see #finalize()* @see Component#getGraphics()* @see Component#paint(Graphics)* @see Component#update(Graphics)* @since 1.0*/public abstract void dispose();/*** Disposes of this graphics context once it is no longer * referenced.** @see #dispose()* @since 1.0*/public void finalize() {dispose();} } from: http://www.cnblogs.com/boring09/p/4274893.html總結(jié)
以上是生活随笔為你收集整理的如何写Java文档注释(Java Doc Comments)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Javadoc注释规范
- 下一篇: 详细聊聊Javadoc注释规范