【问题标题】:How to reference a method in javadoc?如何在 javadoc 中引用方法?
【发布时间】:2011-08-20 10:26:07
【问题描述】:

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

【问题讨论】:

  • 我知道这是几年前的事了,但是……查看官方 Java 类可以帮助您找到所需的任何形式的 Javadoc 格式。例如查看 HashMap Javadoc。

标签: java hyperlink javadoc


【解决方案1】:

您可以在 Documentation Comment Specification for the Standard Doclet 找到很多关于 JavaDoc 的信息,包括关于

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

标签(您正在寻找的)。文档中对应的例子如下

例如,这里有一个引用 getComponentAt(int, int) 方法的注释:

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

如果引用的方法在当前类中,则可以省略module/package.class 部分。


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

【讨论】:

    【解决方案2】:

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

    示例

    同一类中的方法:

    /** 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 不断发展,这可能很脆弱。

    键入擦除和#member

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

    int bar( Collection<Integer> receiver ) { ... }
    
    /** See also {@link #bar(Collection)}. */
    void foo() { ... }
    

    【讨论】:

    • 等等:我只想要带有链接的方法名,我不想要类名。
    • 啊,好吧。上面链接中的第一个示例说明了这一点。
    • +1 用于提供我未从 Oracle JavaDoc HowTo 页面链接到的 Java 6 链接。我仍然无法与 oracle 链接相处...(在我的回答中固定了指向 Java 6 的链接)。
    • @K. Claszen:download.oracle.com/javase/6/docs,然后点击图中的javadoc,然后选择Javadoc Tool Reference Page (Microsoft Windows),然后选择Javadoc tags
    • @Paŭlo Ebermann 谢谢!将来会尝试使用“文档”页面作为入口点。
    【解决方案3】:

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

    【讨论】:

    • OP:“如何使用@link 标签链接到一个方法?”根据 OP 的要求,@link 标记可以在段落内内联使用。 @see 标签不能。在this question 上查看更多详细信息。
    猜你喜欢
    • 2010-12-12
    • 2011-04-06
    • 2016-11-15
    • 2016-03-30
    • 2011-07-12
    • 2013-02-25
    • 2016-04-25
    • 2022-08-22
    • 2013-04-01
    相关资源
    最近更新 更多