首页 文章

如何在javadoc中引用方法?

提问于
浏览
680

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

3 回答

  • 619

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

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

    {@link package.class#member label}

    标签(您正在寻找) . 文档中的相应示例如下

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

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


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

  • 895

    @link section of the javadoc documentation的一般格式为:

    {@link package.class#member label}

    例子

    Method in the same class:

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

    Method in a different class, 在同一个包中或导入:

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

    Method in a different package 并且未导入:

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

    Label linked to method, in plain text 而不是代码字体:

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

    A chain of method calls, 如你的问题所示 . 我们必须为这个类之外的方法的链接指定标签,否则我们得到 getFoo().Foo.getBar().Bar.getBaz() . 但这些标签可能很脆弱;见下面的"Labels" .

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

    标签

    Automated refactoring may not affect labels. 这包括重命名方法,类或包;并更改方法签名 .

    Therefore, provide a label only if you want different text than the default.

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

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

相关问题