文档注释

Java源代码中由 /** ... */ 分隔符分隔的特殊注释。这些注释由Javadoc工具处理以生成API文档。

格式

文档注释使用HTML编写,并且必须在类、字段、构造函数或方法声明之前。它由两部分组成-描述块标记

描述:针对类、字段或者方法进行作用的声明

块标记:类似 @param @return @see

风格指南

对关键字和名称使用 <code>样式

  • Java关键字
  • 包装名称
  • 类名
  • 方法名称
  • 接口名称
  • 栏位名称
  • 参数名称
  • 代码示例
  • 有节制的使用嵌入式连接 {@link} 用户可能实际上想单击它以获取更多信息,并且仅适用于文档注释中每个 API 名称的首次出现。
    通常不必链接到 java.lang 包中的API(例如 String )或你认为总所周知的其他API。
  • 省略方法括号当引用的方法具有多种形式的参数(重载),例如 ArrayList 有两个添加方法: add(Object)add(int, Object)而你只需要引用其中一个特定的方法时,应该使用括号和参数列表。例如只引用 add(int, Object)但是如果同时引用两种方法(及以上),则应该完全省略括号。包括空括号会产生误导,因为这将意味着该方法的特定形式。例如:该add 方法使你可以插入项目
  • 简介起见,可以使用短语而不是完成的句子
  • 使用第三人称而不是第二人称Gets the label. 推荐
    Get the label. 避免
  • 方法描述以动词短语开头Gets the label of this button. 推荐
  • 类、接口、字段描述可以仅声明对象这些API通常描述的是事物而不是动作或行为
    A button label. 推荐
    The field is a button label. 避免
  • 引用当前类创建创建的对象时,使用 this 而不是 theGets the toolkit for this component. 推荐
    Gets the toolkit for the
  • API 名称之外添加说明最好的 API 名称是“自我记录”,意思就是开发人员第一眼看到这个方法的名称基本上就可以知道这个API的作用。如果 doc 注释仅以句子的形式重复 API 名称,那么这个额外的说明就显得没有意义了。
    推荐:




如何生成javadoc文档 生成javadoc开发文档的方法_sed


避免:


如何生成javadoc文档 生成javadoc开发文档的方法_javadoc注释_02


标签顺序

  • @author 仅类和接口必需
  • @version 仅类和接口必需
  • @param 仅方法和构造函数
  • @return 仅方法
  • @exception (@throws 是 Javadoc 1.2中添加的同义词)
  • @see 类和方法
  • @since
  • @serial 说明一个序列化属性
  • @deprecated

多个相同标签

  1. 按时间顺序列出多个 @author 标记,并在顶部列出该类的创建者
  2. 多个 @param 标签应按参数声明顺序列出。这样可以更轻松地将列表与声明进行视觉匹配
  3. 多个 @throws 标记(或 @exception)应按字母顺序按异常名称列出
  4. 多个 @see 标记应按一下顺序排序,基本上是从最近到最远访问,从最低限定到完全限定


如何生成javadoc文档 生成javadoc开发文档的方法_javadoc文档的生成方法_03


必须使用的标签

每个参数都需要一个 @param 标记,即使描述很明显。 @return 标记对于返回除 void 以外的方法都是必需的。

标签讲解

  • @author

作者成员,如果作者不明,请使用 unscribed 作为 @author 的参数

  • @version

版本 例如: 1.39, 02/28/97

  • @param

@param 标记后面跟参数的名称(不是数据类型),后面跟参数的描述。按照惯例,描述中的第一个名词是参数的数据类型。

参数名称按惯例小写。数据类型以小写字母开头表示对象而不是类。如果描述是短语(不包含动词),则描述以小写字母开头;如果是句子,则描述以大写字母开头。仅当短语或者句子后面有句号时,才以句号结尾。


如何生成javadoc文档 生成javadoc开发文档的方法_sed_04


  • @return

对于返回 void 的方法和构造函数,请忽略 @return

  • @deprecated

第一句中的 @deprecated 描述至少应该告诉用户何时不建议使用该 API 以及将其用作代替的内容。 随后的句子也可以解释为什么不推荐使用它。应该包含一个 @see 标记或者 {@link} 标记,该标记指向替换方法:


如何生成javadoc文档 生成javadoc开发文档的方法_sed_05


如果成员没有替代项,则 @deprecated 的参数应为 “无代替”.

  • @since


如何生成javadoc文档 生成javadoc开发文档的方法_sed_06


  • @throws ( @exception 是原始标记)

应为任何已检查的异常都包括一个 @throws 标记

  • @see

一般用于标记该类相关联的类,@see 即可以用在类上,也可用在方法上


如何生成javadoc文档 生成javadoc开发文档的方法_API_07


  • @serial
  • {@link}
  • {@link java.lang.Character} 完全限定的类名
  • {@link String} 省略包名
  • {@link #length()} 省略类名,表示指向当前的某个方法
  • {@link java.lang.String@charAt(int)} 包名.类名.方法名(参数类型)

文档注释示例


/**
* 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();
}
}


参考

Javadoc Tool Home Page

Javadoc 使用详解