Java的:我应该实现抽象方法时,添加一个@Override注释? [英] Java: Should I add an @Override annotation when implementing abstract methods?

查看:1904
本文介绍了Java的:我应该实现抽象方法时,添加一个@Override注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中重写非虚方法,推荐使用@Override注释的,但如果我实现一个抽象的方法是什么?我应该用@Override然后呢?

When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?

推荐答案

我倾向于preFER在这种情况下使用 @覆盖,所以该方法在子类中被标记,如果超类的变化(无论是完全除去的方法中,或改变其签名,等等)。

I tend to prefer the use of @Override in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).

唯一的区别是,没有注释,如果在超类/接口中的方法被改变或删除,所讨论的实现简单地成为该类别的一个正常的方法。因此,你应该添加注释,如果你实现该方法完全履行合同;你可能如果该方法在你的类有道理不应该添加它无论任何实现的接口或继承的抽象方法的。

The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.

这篇关于Java的:我应该实现抽象方法时,添加一个@Override注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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