[英]Java documentation practice for overriding methods whose return type is subclass of overridden method's return type
I am writing Javadoc for a new package where I am facing the dilemma mentioned in the title. 我正在为新程序包编写Javadoc,但遇到了标题中提到的难题。
I have base class method defined as, 我将基类方法定义为
class Vector<E> {
..
public abstract Vector<E> add(Vector<E> v);
..
}
The overriding method is defined as, 覆盖方法定义为
class IntVector extends Vector<Integer> {
..
@Override
public IntVector add(Vector<Integer> v) {
..
}
The overriding method does not change the behavior except for the return type. 除返回类型外,重写方法不会更改行为。 I know that redundant documentation is not desirable for overridden methods. 我知道多余的文档对于覆盖的方法是不理想的。 However in this case it makes sense for the overriding method to have its own documentation, at least for the return type. 但是,在这种情况下,覆盖方法具有自己的文档(至少对于返回类型)是有意义的。 What is the best practice for such situations? 在这种情况下的最佳做法是什么? Just copy the specification over or is there a good way to avoid duplication? 只是复制规范,还是有避免重复的好方法?
As pointed out in the comments, if there is nothing special about the different return type and you only want to point out that it is different, then there is normally no need to do this explicitly. 如评论中所指出的,如果不同的返回类型没有什么特别的,而您只想指出它是不同的,则通常无需显式地执行此操作。 The javadoc and the IDE code completion will indicate that the return type is different. Javadoc和IDE代码完成将指示返回类型不同。
However, if you want to add additional information, then you can take a look at method comment inheritance : 但是,如果要添加其他信息,则可以查看方法注释继承 :
When a main description, or @return, @param, or @throws tag is missing from a method comment, the javadoc command copies the corresponding main description or tag comment from the method it overrides or implements (if any). 当方法注释中缺少主要描述或@ return,@ param或@throws标记时,javadoc命令将从其重写或实现的方法中复制相应的主要描述或标记注释(如果有)。
So in your case you could write: 因此,您可以编写以下代码:
/**
* @return A verify special IntVector
*/
@Override
public IntVector add(Vector<Integer> v) {
...
}
And it would copy all missing information, such as the main description and the documentation for the v
parameter from the overridden method. 并且它将复制所有丢失的信息,例如覆盖的方法的主要描述和v
参数的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.