如何使用
@link
标签链接到方法?
我想改变:
/**
* Returns the Baz object owned by the Bar object owned by Foo owned by this.
* A convenience method, equivalent to getFoo().getBar().getBaz()
* @return baz
*/
public Baz fooBarBaz()
致:
/**
* Returns the Baz object owned by the Bar object owned by Foo owned by this.
* A convenience method, equivalent to {@link getFoo()}.{@link getBar()}.{@link getBaz()}
* @return baz
*/
public Baz fooBarBaz()
但我不知道如何正确格式化
@link
标签。
您将在标准 Doclet 的文档注释规范中找到有关 JavaDoc 的更多信息,包括关于
的信息标签(您正在寻找的)。文档中对应的例子如下
例如,这里有一条引用 getComponentAt(int, int) 方法的注释:
Use the {@link #getComponentAt(int, int) getComponentAt} method.
如果引用的方法在当前类中,
module/package.class
部分可以省略。
有关 JavaDoc 的其他有用链接是:
来自 javadoc 文档的 @link 部分的一般格式是:
同一个类中的方法:
/** See also {@link #myMethod(String)}. */
void foo() { ... }
不同类中的方法,在同一个包中或导入:
/** See also {@link MyOtherClass#myMethod(String)}. */
void foo() { ... }
不同包中的方法且未导入:
/** See also {@link com.mypackage.YetAnotherClass#myMethod(String)}. */
void foo() { ... }
链接到方法的标签,采用纯文本而不是代码字体:
/** See also this {@linkplain #myMethod(String) implementation}. */
void foo() { ... }
一系列方法调用,如您的问题所示。我们必须为此类之外的方法的链接指定标签,否则我们会得到
getFoo().Foo.getBar().Bar.getBaz()
。但这些标签在重构过程中可能很脆弱——请参阅下面的“标签”。
/**
* A convenience method, equivalent to
* {@link #getFoo()}.{@link Foo#getBar() getBar()}.{@link Bar#getBaz() getBaz()}.
* @return baz
*/
public Baz fooBarBaz()
自动重构可能不会影响标签。这包括重命名方法、类或包;并更改方法签名。
因此,如果您想要与默认文本不同的文本,请仅提供标签。
例如,您可以从人类语言链接到代码:
/** You can also {@linkplain #getFoo() get the current foo}. */
void setFoo( Foo foo ) { ... }
或者您可以从具有与默认文本不同的文本的代码示例进行链接,如上面“方法调用链”下所示。然而,随着 API 的不断发展,这可能很脆弱。
如果方法签名包含参数化类型,请在 javadoc @link 中使用这些类型的擦除。例如:
int bar( Collection<Integer> receiver ) { ... }
/** See also {@link #bar(Collection)}. */
void foo() { ... }
您可以使用
@see
来做到这一点:
样本:
interface View {
/**
* @return true: have read contact and call log permissions, else otherwise
* @see #requestReadContactAndCallLogPermissions()
*/
boolean haveReadContactAndCallLogPermissions();
/**
* if not have permissions, request to user for allow
* @see #haveReadContactAndCallLogPermissions()
*/
void requestReadContactAndCallLogPermissions();
}