JavaDoc中的`.`和`#`有什么区别?

3

我在方法上方的 Javadoc 中有这样一行:

* {@link client.navigator.URLManager.newToken(NavigationToken)}

当我写代码时,Intellij IDEA会将其标记为错误:

无法解析符号'client.navigator.URLManager.newToken'

但如果我将.改为#,它就可以正常运行。

* {@link client.navigator.URLManager#newToken(NavigationToken)}

有什么区别吗?因为我在项目中有很多地方用到 .#


client.navigator.URLManager 是该类的完整路径,包括其命名为 client.navigator 的包。就像在您的代码中导入它一样,例如 import client.navigator.URLManager;。然而,newToken 是一个方法,Javadoc标准建议使用 # 来分隔类和其方法。 - Zabuzard
2个回答

7

.分隔包的不同部分以及包与类之间的关系。

#则用于区分类名和字段、方法或构造函数。

例如,在client.navigator.URLManager#newToken中,client.navigator是一个包,URLManager是一个类,而newToken则是方法名。

甚至可以使用#someMethod来引用当前类中的方法,而无需指定类(对于字段也是一样)。

需要注意的是,对于内部类,会有多个类名:java.lang.Thread.State是一个内部类,它位于Thread内部,其名称为State,所处的包为java.lang。在语法上,内部类与顶级类没有区别,唯一的方式是通过观察Thread是否大写来推断(尽管Java允许小写类和大写包,即使惯例禁止这样做)。


0

Javadoc生成时在构建链接时会注意到".""#"字符。链接可以指向其他Javadoc页面或特定Javadoc页面内的位置。

例如,这里有一个来自Integer"#"示例,其中包含了这个Javadoc指令:{@link java.lang.Integer#toString(int)}。当Javadoc文件被生成时,该"link"指令将被评估为HTML,指向"Integer" .html页面上的toString()方法(具体为:https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#toString--),所以你可以直接跳转到那里:

/**
 * Returns a {@code String} object representing this
 * {@code Integer}'s value. The value is converted to signed
 * decimal representation and returned as a string, exactly as if
 * the integer value were given as an argument to the {@link
 * java.lang.Integer#toString(int)} method.
 *
 * @return  a string representation of the value of this object in
 *          base 10.
 */
public String toString() {
    return toString(value);
}

这是另一个例子,只有"."字符(没有"#"),来自于String,它有一个链接({@link java.text.Collator}),生成一个URL(https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/Collator.html),跳转到java.text.Collator页面的顶部
 * The class {@code String} includes methods for examining

...

 * <p>Unless otherwise noted, methods for comparing Strings do not take locale
 * into account.  The {@link java.text.Collator} class provides methods for
 * finer-grain, locale-sensitive String comparison.

...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接