如何在javadoc中引用方法?

本文翻译自:How to reference a method in javadoc?

How can I use the @link tag to link to a method? 如何使用@link标记链接到方法?

I want to change 我想改变

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

to

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

but I don't know how to format the @link tag correctly. 但我不知道如何正确格式化@link标签。


#1楼

参考:https://stackoom.com/question/Op1E/如何在javadoc中引用方法


#2楼

you can use @see to do that: 你可以使用@see来做到这一点:

sample: 样品:

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();
    }

#3楼

The general format, from the @link section of the javadoc documentation , is: 来自javadoc文档@link部分的一般格式是:

{@link package.class#member label}

Examples 例子

Method in the same class: 同一类中的方法:

/** See also {@link #myMethod(String)}. */
void foo() { ... }

Method in a different class, either in the same package or imported: 不同类中的方法在同一个包中或导入:

/** See also {@link MyOtherClass#myMethod(String)}. */
void foo() { ... }

Method in a different package and not imported: 不同包中的方法而不导入:

/** See also {@link com.mypackage.YetAnotherClass#myMethod(String)}. */
void foo() { ... }

Label linked to method, in plain text rather than code font: 标签链接到方法,以纯文本而不是代码字体:

/** See also this {@linkplain #myMethod(String) implementation}. */
void foo() { ... }

A chain of method calls, as in your question. 一系列方法调用,如您的问题所示。 We have to specify labels for the links to methods outside this class, or we get getFoo().Foo.getBar().Bar.getBaz() . 我们必须为这个类之外的方法指定链接的标签,或者我们得到getFoo().Foo.getBar().Bar.getBaz() But these labels can be fragile; 但这些标签可能很脆弱; see "Labels" below. 请参阅下面的“标签”。

/**
 * A convenience method, equivalent to 
 * {@link #getFoo()}.{@link Foo#getBar() getBar()}.{@link Bar#getBaz() getBaz()}.
 * @return baz
 */
public Baz fooBarBaz()

Labels 标签

Automated refactoring may not affect labels. 自动重构可能不会影响标签。 This includes renaming the method, class or package; 这包括重命名方法,类或包; and changing the method signature. 并更改方法签名。

Therefore, provide a label only if you want different text than the default. 因此, 当您需要与默认文本不同的文本时,才提供标签。

For example, you might link from human language to code: 例如,您可以从人类语言链接到代码:

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

Or you might link from a code sample with text different than the default, as shown above under "A chain of method calls." 或者您可以从代码示例链接到不同于默认值的文本,如上面“方法调用链”中所示。 However, this can be fragile while APIs are evolving. 但是,当API不断发展时,这可能很脆弱。

Type erasure and #member 键入擦除和#member

If the method signature includes parameterized types, use the erasure of those types in the javadoc @link. 如果方法签名包含参数化类型,请在javadoc @link中使用这些类型的擦除。 For example: 例如:

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

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

#4楼

You will find much information about JavaDoc at the Documentation Comment Specification for the Standard Doclet , including the information on the 您可以在标准Doclet文档注释规范中找到有关JavaDoc的更多信息,包括有关的信息

{@link package.class#member label} {@link package.class#member label}

tag (that you are looking for). 标签(您正在寻找)。 The corresponding example from the documentation is as follows 文档中的相应示例如下

For example, here is a comment that refers to the getComponentAt(int, int) method: 例如,这是一个引用getComponentAt(int,int)方法的注释:

Use the {@link #getComponentAt(int, int) getComponentAt} method.

The package.class part can be ommited if the referred method is in the current class. 如果引用的方法在当前类中,则可以省略package.class部分。


Other useful links about JavaDoc are: 关于JavaDoc的其他有用链接是:

发布了0 篇原创文章 · 获赞 75 · 访问量 56万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105430960