Java 文档注释


Java文档注释

Java文档注释是用于描述Java代码的标准注释格式,能够自动提取出代码中的文档信息并生成文档页面。这种注释经常出现在开源Java代码中,并广泛应用于Java程序员之间的协作。

注释格式

Java文档注释的格式非常简单,以/**开头,*/结尾,中间可以添加多个*,同时具有高度的可自定义性,下面是一个常见的Java文档注释格式的例子:

/**
 * The Main class of this Hello World Program.
 * It will print "Hello, World!" when run.
 */
public class Main {
    /**
     * This is the main method of the program.
     * @param args The command line arguments.
     */
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Java文档注释有几个特点:

  • 要放在被注释方法或者变量的上方
  • 以"/**“开头,”*/“结尾
  • 要用”@“符号标注变量或方法的含义
  • 注释要以英文写作,避免中文注释

具体内容,我们将在下面继续解释。

注释内容

Java文档注释中可以添加很多内容,以帮助开发人员更好的了解代码意图。在实际应用中,大部分Java文档注释都包含以下内容:

代码示例

Java文档注释中可以添加代码示例,用于展现被注释方法或者变量的正确使用方法。如下面例子展示:

/**
 * A class representing a book.
 * @version 1.0
 */
public class Book {
    private String title;   // The title of the book.
    private String author;  // The author of the book.

    /**
     * Creates a new Book object with the specified title and author.
     * @param title The title of the book.
     * @param author The author of the book.
     */
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    /**
     * Returns the title of the book.
     * @return The title of the book.
     * @example "The Great Gatsby", "1984"
     */
    public String getTitle() {
        return title;
    }

    /**
     * Returns the author of the book.
     * @return The author of the book.
     * @example "F. Scott Fitzgerald", "George Orwell"
     */
    public String getAuthor() {
        return author;
    }
}

参数说明

Java文档注释可以用@param标签来标注方法或者函数的参数说明。如下面的例子:

/**
 * This is a method to calculate the area of a rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @return The area of the rectangle.
 */
public int getArea(int width, int height) {
    return width * height;
}

返回值说明

Java文档注释可以用@return标签来标注带有返回值的函数的返回值说明。如:

/**
 * This is a method to calculate the area of a rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @return The area of the rectangle.
 */
public int getArea(int width, int height) {
    return width * height;
}

异常说明

Java文档注释可以用@throws标签来标注函数抛出的异常。如:

/**
 * This method may throw an IllegalArgumentException if the input string is null.
 * @param input A string to check.
 * @throws IllegalArgumentException if the input string is null. 
 */
public void test(String input) throws IllegalArgumentException {
    if(input == null) {
        throw new IllegalArgumentException("The input string cannot be null.");
    }
}

其他信息

Java文档注释还可以包含其他类型的信息,比如作者,创建日期,版本号等等。我们通常使用@author标签:

/**
 * The Main class of this Hello World Program.
 * It will print "Hello, World!" when run.
 * @author John
 */
public class Main {
    /**
     * This is the main method of the program.
     * @param args The command line arguments.
     */
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

同时,可以用@version标签表示版本号:

/**
 * A class representing a book.
 * @version 1.0
 */
public class Book {
    private String title;   // The title of the book.
    private String author;  // The author of the book.

    /**
     * Creates a new Book object with the specified title and author.
     * @param title The title of the book.
     * @param author The author of the book.
     */
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    /**
     * Returns the title of the book.
     * @return The title of the book.
     * @example "The Great Gatsby", "1984"
     */
    public String getTitle() {
        return title;
    }

    /**
     * Returns the author of the book.
     * @return The author of the book.
     * @example "F. Scott Fitzgerald", "George Orwell"
     */
    public String getAuthor() {
        return author;
    }
}

总结

通过这篇技术文档,我们深入了解了Java文档注释的格式和内容,深入理解Java文档注释的重要性和优越性。Java文档注释可以增进代码的可读性和协作性,同时也为代码的后续维护和升级提供了良好的保障,是我们工作中必须掌握的一项高级技能。