简体   繁体   English

Java文档实践,其重写方法的返回类型是重写方法的返回类型的子类

[英]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.

相关问题 Java泛型 - 覆盖抽象方法并具有子类的返回类型 - Java generics - overriding an abstract method and having return type of the subclass 如何返回参数化返回类型的子类,其参数类型是返回类型的参数类型的子类? - How to return a subclass of parameterized return type whose parametric type is a subclass of the return type's parametric type? 继承规则-覆盖方法的返回类型可以是在覆盖方法中声明的返回类型的子类 - Rule for inheritance — return type of overriding method can be child class of return type declared in overridden method 重写方法所需的返回类型 - return type required for overridden method Java中具有通用返回类型的重写方法 - Overriding method with generic return type in Java 类型安全返回泛型子类的Java方法 - Java method for type safe return generic subclass 为什么Java为重写的静态方法强制执行返回类型兼容性? - Why does Java enforce return type compatibility for overridden static methods? 如果重写方法的返回类型是原始的(例如double),我们可以更改重写方法的返回类型(例如int,char)吗? - if overridden method's return type is primitive (e.g. double) ,can we change return type of overriding method (e.g. int ,char)? 用返回类型调用方法的Java最佳实践 - Java best practice for calling method with return type 重写的方法可以在返回类型上有所不同吗? - Can overridden methods differ in return type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM