亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java - 文档注释

Java 语言支持三种类型的注释 -

Sr.No. 评论和说明
1

/* 文字 */

编译器会忽略从 /* 到 */ 的所有内容。

2

//文字

编译器会忽略从 // 到行尾的所有内容。

3

/** 文档 */

这是一个文档注释,通常称为 doc comment . JDK javadoc 工具使用 doc 注释 在准备自动生成的文档时。

本章都是关于解释 Javadoc。我们将了解如何利用 Javadoc 为 Java 代码生成有用的文档。

什么是 Javadoc?

Javadoc是JDK自带的一个工具,用于从Java源代码生成HTML格式的Java代码文档,需要预定义格式的文档。

以下是一个简单的示例,其中 /*….*/ 中的行是 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!");
   }
}

您可以在描述部分中包含所需的 HTML 标记。例如,以下示例使用

....

作为标题,而

已用于创建分节符 -

示例

/**
* <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 工具可识别以下标签 -

标签 描述 语法
@author 添加类的作者。 @作者姓名-文本
{@code} 以代码字体显示文本,而不将文本解释为 HTML 标记或嵌套的 javadoc 标记。 {@code text}
{@docRoot} 表示从任何生成的页面到生成的文档的根目录的相对路径。 {@docRoot}
@deprecated 添加注释,表明不应再使用此 API。 @deprecated deprecatedtext
@exception 添加一个投掷 生成文档的副标题,带有类名和描述文本。 @exception 类名描述
{@inheritDoc} 最近的继承评论 可继承的类或可实现的接口。 从直接超类继承注释。
{@link} 插入带有可见文本标签的内联链接,该标签指向指定包、类或引用类的成员名称的文档。 {@link package.class#member label}
{@linkplain} 与 {@link} 相同,只是链接的标签以纯文本而不是代码字体显示。 {@linkplain package.class#member label}
@param 将具有指定参数名称和指定描述的参数添加到“参数”部分。 @param 参数名描述
@return 添加带有描述文本的“退货”部分。 @return 描述
@see 添加一个“另见”标题,其中包含指向参考的链接或文本条目。 @见参考
@serial 用于默认可序列化字段的文档注释中。 @serial field-description |包括 |排除
@serialData 记录由 writeObject( ) 或 writeExternal( ) 方法写入的数据。 @serialData 数据描述
@serialField 记录一个 ObjectStreamField 组件。 @serialField 字段名字段类型字段描述
@since 在生成的文档中添加一个带有指定 since 文本的“Since”标题。 @since release
@throws @throws 和@exception 标签是同义词。 @throws 类名描述
{@value} 在静态字段的文档注释中使用 {@value} 时,会显示该常量的值。 {@value package.class#field}
@version 当使用 -version 选项时,将带有指定版本文本的“版本”副标题添加到生成的文档中。 @version 版本文本

示例

以下程序使用了一些可用于文档注释的重要标签。您可以根据自己的需要使用其他标签。

关于 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.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
$

您可以在此处查看所有生成的文档 - AddNum。如果您使用的是 JDK 1.7,那么 javadoc 不会生成出色的 stylesheet.css ,所以我们建议从 https://docs.oracle.com/javase/7/docs/api/stylesheet.css 下载和使用标准样式表


java

  1. Java 运算符
  2. Java 评论
  3. Java if...else 语句
  4. Java for-each 循环
  5. Java 字符串
  6. Java 接口
  7. Java 匿名类
  8. Java try-with-resources
  9. Java 注释
  10. Java 断言
  11. Java 向量
  12. Java - 文档注释