小编典典
第一种形式称为Javadoc。在为javadoc工具编写由程序生成的正式API时,可以使用此功能。例如,Java
7 API页面使用Javadoc并由该工具生成。
您将在Javadoc中看到的一些常见元素包括:
@param:用于指示将哪些参数传递给方法,以及它们应具有的值
@return:用于指示该方法将返回的结果
@throws:用于指示在某些输入的情况下方法抛出异常或错误
@since:用于表示该类或函数的最早Java版本。
例如,以下是Javadoc的compare方法Integer:
/**
* Compares two {@code int} values numerically.
* The value returned is identical to what would be returned by:
*
* Integer.valueOf(x).compareTo(Integer.valueOf(y))
*
*
* @param x the first {@code int} to compare
* @param y the second {@code int} to compare
* @return the value {@code 0} if {@code x == y};
* a value less than {@code 0} if {@code x < y}; and
* a value greater than {@code 0} if {@code x > y}
* @since 1.7
*/
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
第二种形式是块(多行)注释。如果要在注释中包含多行,请使用此选项。
我会说,你只需要使用后者的形式 谨慎 ; 也就是说,您不想用不描述方法/复杂函数应该具有的行为的块注释来使代码负担重。
由于Javadoc是两者中更具描述性的,并且您可以通过使用Javadoc生成实际文档,因此使用Javadoc比简单的块注释更可取。
2020-09-11