Java 注释
在一个程序中,注释就像人的缩进一样,它们的作用是让不熟悉该语言的人更容易理解代码。它也会使你,作为一个编码员,更容易发现代码中的错误,因为你将很容易找到错误的位置。编译器在编译代码时忽略了注释,从长远来看,这使得工作更加复杂,因为他们必须通过这么多的代码才能找到一行。
Java 有三种类型的注释
- 单行注释。
- 多行注释。
- 文档注释。
A. 单行注释
一个初级的程序员主要使用单行注释来描述代码的功能。这是最简单的类型注释。
语法
//Comments here( Text in this line only is considered as comment )
示例:
实际代码中的例子
例子
// Java program to show single line comments
class Scomment
{
public static void main(String args[])
{
// Single line comment here
System.out.println("Single line comment above");
}
}
输出
Single line comment above
B. 多行注释
要在代码中描述一个完整的方法或一个复杂的片段,单行注释的编写是很乏味的,因为我们必须在每一行都给出’//’。所以为了克服这个问题,可以使用多行注释。
语法
/*Comment starts
continues
continues
.
.
.
Comment ends*/
例子
//Java program to show multi line comments
class Scomment
{
public static void main(String args[])
{
System.out.println("Multi line comments below");
/*Comment line 1
Comment line 2
Comment line 3*/
}
}
输出
Multi line comments below
我们也可以通过使用上述语法完成单行注释,如下图所示。
/*Comment line 1*/
C. 文档注释
这种类型的注释一般在编写项目/软件包的代码时使用,因为它有助于生成一个供参考的文档页,可以用来获取关于存在的方法、其参数等信息。例如,http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 是一个自动生成的文档页面,它是通过使用文档注释和处理注释的javadoc工具生成的。
语法
/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used
*such as <h1>
*
*comment ends*/
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
/**
comment line 1
comment line 2
*/
}
}
可使用的标签。
标签 | 描述 | 语法 |
---|---|---|
@author | 添加一个类的作者。 | @author name-text |
{@code} | 以代码字体显示文本,而不把文本解释为HTML标记或嵌套的javadoc标签。 | {@code text} |
{@docRoot} | 代表从任何生成的页面到生成文档的根目录的相对路径。 | {@docRoot} |
@deprecated | 添加一个注释,表明这个API不应该再被使用。 | @deprecated 废弃的文本 |
@exception | 在生成的文档中添加一个Throws的小标题,包括类名和描述文本。 | @exception class-name description |
{@inheritDoc} | 继承最近的可继承类或可实现接口的注释。 | 继承直接的surperclass的注释。 |
{@link} | 插入一个带有可见文本标签的行内链接,指向指定的包、类或被引用类的成员名称的文档。 | {@link包.类#成员标签} |
{@linkplain} | 与{@link}相同,只是链接的标签是用纯文本而不是代码字体显示。 | {@linkplain 包.类#成员标签} |
@param | 在 “参数 “部分添加一个参数,并在后面加上指定的描述。 | @param parameter-name description |
@return | 添加一个带有描述文字的 “返回 “部分。 | @return 描述 |
@see | 添加一个 “See Also “标题,带有指向参考的链接或文字条目。 | @see reference |
@serial | 用于默认可序列化字段的文档注释中。 | @serial field-description | include | exclude |
@serialData | 记录由 writeObject( ) 或 writeExternal( ) 方法写入的数据。 | @serialData data-description |
@serialField | 记录一个ObjectStreamField组件。 | @serialField field-name field-type field-description |
@since | 在生成的文档中添加一个带有指定的 since-text 的标题 “自”。 | @since release |
@throws | @throws和@exception标签是同义词。 | @throws类名描述 |
{@value} | 当{@value}在静态字段的文档注释中使用时,它显示的是该常量的值。 | {@value package.class#field} |
@version | 当使用-version选项时,在生成的文档中添加一个带有指定版本文字的 “版本 “子标题。 | @version 版本-文本 |
实现
// Java program to illustrate frequently used
// Comment tags
/**
* <h1>Find average of three numbers!</h1>
* The FindAvg program implements an application that
* simply calculates average of three integers and Prints
* the output on the screen.
*
* @author Pratik Agarwal
* @version 1.0
* @since 2017-02-18
*/
public class FindAvg
{
/**
* This method is used to find average of three integers.
* @param numA This is the first parameter to findAvg method
* @param numB This is the second parameter to findAvg method
* @param numC This is the second parameter to findAvg method
* @return int This returns average of numA, numB and numC.
*/
public int findAvg(int numA, int numB, int numC)
{
return (numA + numB + numC)/3;
}
/**
* This is the main method which makes use of findAvg method.
* @param args Unused.
* @return Nothing.
*/
public static void main(String args[])
{
FindAvg obj = new FindAvg();
int avg = obj.findAvg(10, 20, 30);
System.out.println("Average of 10, 20 and 30 is :" + avg);
}
}
输出
Average of 10, 20 and 30 is :20
对于上述代码,可以通过使用工具 “javadoc “来生成文档。
Javadoc可以通过在终端运行以下命令来使用。
javadoc FindAvg.java