如何在Javadoc中引用同一类中的另一个方法?

51

假设你的类有两个方法:

contains() and
containsSame()

它们之间的区别微妙,您希望将其作为Javadoc的一部分提及。

在Javadoc中,如何通过名称引用同一类中的一个方法?


可能是javadoc:编写到方法的链接的重复问题。 - Simon
1个回答

85

使用@link内联标签,并在方法前加上#引用该方法。

/**
 * ...
 * This method is similar to {@link #contains()}, with the following differences:
 * ...
 */
public boolean containsSame();


/**
 * This method does ...
 */
public boolean contains();

如果实际上存在一个没有参数的contains()方法(实际上似乎并不那么有用),那么这个示例才能正常工作。如果你只有一个带参数的contains方法,那么在括号内写入参数类型:

/**
 * ...
 * This method is similar to {@link #contains(Element)}, with the following differences:
 * ...
 */
public boolean containsSame(Element e);

/**
 * This method does ...
 */
public boolean contains(Element e);

或者您完全可以省略括号:

/**
 * ...
 * This method is similar to {@link #contains}, with the following differences:
 * ...
 */
public boolean containsSame(Element e);

/**
 * This method does ...
 */
public boolean contains(Element e);
如果您有多个名为contains的方法(具有不同的参数列表),则此版本无法决定使用哪一个(链接将跳转到其中任意一个,希望它们都在一起并执行类似的操作)。

提供一点提示:我在使用上面的答案时收到了一个警告。将方法中的括号去掉即可解决:{@link #contains} - Pytry
1
@Pytry 我猜你的方法实际上有参数?我更新了答案以反映这一点。 - Paŭlo Ebermann
扇脸 ... 你说的对。你的详细解释很好。再次感谢! - Pytry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接