是否可以覆盖方法注释,但不能覆盖方法? [英] Is it possible to override a method comment but not the method?

查看:74
本文介绍了是否可以覆盖方法注释,但不能覆盖方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个类中都有方法 toSaveString(StringBuilder) toSaveString(),并考虑过将它们转换为接口.第一个方法将始终必须实现,第二个方法可以 default ,因为它基本上每次每次都使用新的字符串生成器调用第一个方法并返回生成的字符串.(不是 default 的用途,但请忍受.)

I have methods toSaveString(StringBuilder) and toSaveString() in several classes and thought of turning those into an interface. The first method would always have to be implemented and the second I could default because it basically only calls the first method every time with a new string builder and returns the resulting string. (Not what default is designed for, but bear with me.)

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

Now I wouldn't need to implement toSaveString() in the classes implementing the interface, but I would like to change its documentation nonetheless to match the class. Is there a way to achieve this without overriding the toSaveString() method in the implementing class? Because adding three lines to call the default method or five to copy the implementation seems redundant and easy to get errors mixed in.

也可以在此处对设计替代方案发表评论,但问题仍然存在,因为它本身很有趣.

Also feel free to leave comments about design alternatives here, but the question stays because it is interesting in its own right.

推荐答案

  1. 查看

  2. 它会覆盖其超类

  3. 在您的情况下,您只能 override javadoc ,并在方法主体中编写如下内容:

  4. In your case, you can override only javadoc, and write something like this in the method body:

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


  5. 另请参见:我可以在不重写整个方法的情况下向继承的方法添加代码吗?

    这篇关于是否可以覆盖方法注释,但不能覆盖方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆