【问题标题】:Is it possible to override a method comment but not the method?是否可以覆盖方法注释但不能覆盖方法?
【发布时间】:2021-05-24 15:02:42
【问题描述】:

我在几个类中有方法toSaveString(StringBuilder)toSaveString(),我想把它们变成一个接口。第一个方法总是必须实现,第二个我可以default,因为它基本上每次只用一个新的字符串生成器调用第一个方法并返回结果字符串。 (不是 default 的用途,但请耐心等待。)

现在我不需要在实现接口的类中实现toSaveString(),但我仍想更改其文档以匹配该类。 有没有办法在不覆盖实现类中的toSaveString() 方法的情况下实现这一目标?因为添加三行来调用默认方法或添加五行来复制实现似乎是多余的,而且容易混入错误。

您也可以在这里留下关于设计替代方案的 cmets,但问题仍然存在,因为它本身就很有趣。

【问题讨论】:

  • 顺便说一句,接口在我的代码中确实有一个用例。在某一时刻,我的代码中有四种方法是相同的,只是它们在不同的类上调用 toSaveString(StringBuilder)。很容易出错 imo
  • 如果您对问题有澄清,编辑问题并澄清,不要写评论。删除评论。
  • 回答:不行,你只能通过实际编写方法声明,即实现方法来为方法编写javadoc。
  • @Andreas 我看到你很有趣。本着您的精神,您不会将您的答案作为实际答案而不是评论发布吗?
  • 也许如果你更新你的问题,他可能会 :-) 但是,答案是“不,你不能那样做”形式的问题往往会吸引反对票,而嘈杂的替代答案可能会错过问题的重点或提出无意义的建议。 (您在最后一句中的邀请只会鼓励这样做......)

标签: java inheritance methods comments


【解决方案1】:
  1. 查看ArrayList#removeIf方法的javadoc

    /**
     * @throws NullPointerException {@inheritDoc}
     */
    @Override
    public boolean removeIf(Predicate<? super E> filter) {
        return removeIf(filter, 0, size);
    }
    
  2. 它覆盖了它的超类 Collection#removeIf 方法:

    /**
     * Removes all of the elements of this collection that satisfy the given
     * predicate.  Errors or runtime exceptions thrown during iteration or by
     * the predicate are relayed to the caller.
     *
     * @implSpec
     * The default implementation traverses all elements of the collection using
     * its {@link #iterator}.  Each matching element is removed using
     * {@link Iterator#remove()}.  If the collection's iterator does not
     * support removal then an {@code UnsupportedOperationException} will be
     * thrown on the first matching element.
     *
     * @param filter a predicate which returns {@code true} for elements to be
     *        removed
     * @return {@code true} if any elements were removed
     * @throws NullPointerException if the specified filter is null
     * @throws UnsupportedOperationException if elements cannot be removed
     *         from this collection.  Implementations may throw this exception if a
     *         matching element cannot be removed or if, in general, removal is not
     *         supported.
     * @since 1.8
     */
    default boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator<E> each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }
    
  3. 在您的情况下,您只能覆盖 javadoc,并在方法主体中编写如下内容:

    /**
     * custom javadoc
     */
    @Override
    public boolean customMethod(Object parameter) {
        return super.customMethod(parameter);
    }
    

另见:Can I add code to an inherited method without overriding the whole method?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2012-10-26
    相关资源
    最近更新 更多