eclipse Java 文档覆盖方法不 InheritDoc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1081408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Documentation Override Method does not InheritDoc
提问by Verhogen
A method that overrides another method does not inherit documentation of the method it is overriding. Is there any way to explicitly tell it to inherit the documentation?
覆盖另一个方法的方法不会继承它正在覆盖的方法的文档。有什么方法可以明确告诉它继承文档吗?
/**
* {@inheritDoc}
*
* This implementation uses a dynamic programming approach.
*/
@Override
public int[] a(int b) {
return null;
}
回答by Brandon E Taylor
According to the javadoc documentation:
根据javadoc 文档:
Inheriting of comments occurs in all three possible cases of inheritance from classes and interfaces:
- When a method in a class overrides a method in a superclass
- When a method in an interface overrides a method in a superinterface
- When a method in a class implements a method in an interface
注释的继承发生在从类和接口继承的所有三种可能的情况中:
- 当类中的方法覆盖超类中的方法时
- 当接口中的方法覆盖超接口中的方法时
- 当类中的方法实现接口中的方法时
Comments can be explicitly inherited using the {@inheritDoc}tag. If no comments are provided for an overriding method, comments will be implicitly inherited. Aspects of the inheriting comments (e.g. params, return value, etc.) can be overridden if you so desire.
可以使用{@inheritDoc}标签显式继承注释。如果没有为覆盖方法提供注释,注释将被隐式继承。如果您愿意,可以覆盖继承注释的方面(例如参数、返回值等)。
Importantly, you need to make sure that the source file containing the code with the comment to be inherited is available to the javadoc tool. You can do this by using the -sourcepathoption.
重要的是,您需要确保包含要继承的注释的代码的源文件可用于 javadoc 工具。您可以使用 - sourcepath选项来执行此操作。
回答by corlettk
Algorithm for Inheriting Method Comments- If a method does not have a doc comment, or has an {@inheritDoc} tag, the Javadoc tool searches for an applicable comment using the following algorithm, which is designed to find the most specific applicable doc comment, giving preference to interfaces over superclasses:
- Look in each directly implemented (or extended) interface in the order they appear following the word implements (or extends) in the method declaration. Use the first doc comment found for this method.
- If step 1 failed to find a doc comment, recursively apply this entire algorithm to each directly implemented (or extended) interface, in the same order they were examined in step 1.
- If step 2 failed to find a doc comment and this is a class other than Object (not an interface): 1. If the superclass has a doc comment for this method, use it. 2. If step 3a failed to find a doc comment, recursively apply this entire algorithm to the superclass.
继承方法注释的算法- 如果一个方法没有 doc 注释,或者有 {@inheritDoc} 标签,Javadoc 工具使用以下算法搜索适用的注释,该算法旨在找到最具体的适用的 doc 注释,优先考虑接口而不是超类:
- 按照它们在方法声明中的单词实现(或扩展)之后出现的顺序查看每个直接实现(或扩展)的接口。使用为此方法找到的第一个文档注释。
- 如果第 1 步未能找到文档注释,则将整个算法递归地应用于每个直接实现(或扩展)的接口,与它们在第 1 步中检查的顺序相同。
- 如果第 2 步未能找到文档注释并且这是一个除 Object 之外的类(不是接口): 1. 如果超类具有此方法的文档注释,请使用它。2. 如果步骤 3a 未能找到文档注释,则将整个算法递归应用于超类。
I believe (though I could be wrong) that this basic algorithm still applies to Java 1.5 and 1.6... though it really would be awfully nice of Sun to publish a complete self-contained definitive document for each version of the toolset... I guess it's an overhead they just can't afford, atleast for a free toolset.
我相信(尽管我可能是错的)这个基本算法仍然适用于 Java 1.5 和 1.6 ......尽管Sun 为每个版本的工具集发布一个完整的自包含最终文档真的非常好......我想这是他们负担不起的开销,至少对于免费的工具集。
Cheers. Keith.
干杯。基思。
Edit:
编辑:
Here's a quick and dirty example.
这是一个快速而肮脏的例子。
Code
代码
package forums;
interface Methodical
{
/**
* A no-op. Returns null.
* @param i int has no effect.
* @return int[] null.
*/
public int[] function(int i);
}
interface Methodological extends Methodical
{
/**
* Another no-op. Does nothing.
*/
public void procedure();
}
class Parent implements Methodological
{
@Override
public int[] function(int i) {
return null;
}
@Override
public void procedure() {
// do nothing
}
}
class Child extends Parent
{
/** {@inheritDoc} */
@Override
public int[] function(int i) {
return new int[0];
}
/** {@inheritDoc} */
@Override
public void procedure() {
System.out.println("I'm a No-op!");
}
}
public class JavaDocTest
{
public static void main(String[] args) {
try {
new Child().procedure();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Javadoc
Javadoc
C:\Java\home\src\forums>javadoc -package -sourcepath . JavaDocTest.java
Loading source file JavaDocTest.java...
Constructing Javadoc information...
Standard Doclet version 1.6.0_12
Building tree for all the packages and classes...
Generating forums/\Child.html...
Generating forums/\JavaDocTest.html...
Generating forums/\Methodical.html...
Generating forums/\Methodological.html...
Generating forums/\Parent.html...
Generating forums/\package-frame.html...
Generating forums/\package-summary.html...
Generating forums/\package-tree.html...
Generating constant-values.html...
Building index for all the packages and classes...
Generating overview-tree.html...
Generating index-all.html...
Generating deprecated-list.html...
Building index for all classes...
Generating allclasses-frame.html...
Generating allclasses-noframe.html...
Generating index.html...
Generating help-doc.html...
Generating stylesheet.css...
Producesfile:///C:/Java/home/src/forums/index.html
生成file:///C:/Java/home/src/forums/index.html
function
public int[] function(int i)
A no-op. Returns null.
Specified by:
function in interface Methodical
Overrides:
function in class Parent
Parameters:
i - int has no effect.
Returns:
int[] null.
procedure
public void procedure()
Another no-op. Does nothing.
Specified by:
procedure in interface Methodological
Overrides:
procedure in class Parent
回答by user1648825
Swap @Override with javaDoc.
用 javaDoc 交换 @Override。
@Override
/**
* {@inheritDoc}
*/