问题

如何使用@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标签。


#1 热门回答(824 赞)

你可以在JavaDoc Tool reference page找到有关JavaDoc的更多信息,包括有关的信息

{@link package.class#member label}

标签(你正在寻找):

例如,这是一个引用getComponentAt(int,int)方法的注释:使用{@link #getComponentAt(int,int)getComponentAt}方法。

关于JavaDoc的其他有用链接是:

  • JavaDoc技术
  • 如何为Javadoc工具编写Doc注释

#2 热门回答(576 赞)

一般格式,来自@link section of the javadoc documentation,是:

{@link package.class#member label}

例子

同一类中的方法:

/**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()

标签

2882​​74623自动重构可能不会影响标签。**这包括重命名方法,类或包;并更改方法签名。

因此,如果你想要与默认文本不同的文本,请提供标签。

例如,你可以从人类语言链接到代码:

/**You can also {@linkplain #getFoo() get the current foo}. */
void setFoo( Foo foo ) { ... }

或者你可以从代码示例链接到不同于默认值的文本,如上面“方法调用链”中所示。但是,当API不断发展时,这可能很脆弱。

#键入擦除和#member

如果方法签名包含参数化类型,请在javadoc @link中使用这些类型的擦除。例如:

int bar( Collection<Integer> receiver ) { ... }

/**See also {@link #bar(Collection)}. */
void foo() { ... }

#3 热门回答(10 赞)

你可以使用@ 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();
    }

原文链接