我们应该@Override一个接口的方法实现? [英] Should we @Override an interface's method implementation?
问题描述
应该实现一个接口方法, @覆盖
?
Should a method that implements an interface method be annotated with @Override
?
在覆盖的的javadoc
标注说:
表示一个方法声明打算重写超类中的方法声明。如果一个方法被标注了这个注释类型,但不覆盖超类的方法,编译器都需要产生一个错误消息。
Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.
我不认为一个接口在技术上是一个超类。或者是什么?
I don't think that an interface is technically a superclass. Or is it?
<大骨节病> 问题拟订 骨节病>
推荐答案
您应尽可能使用@Override。它prevents被做简单的错误。例如:
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}
这不会编译,因为它不能正确地覆盖<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)\"><$c$c>public布尔等于(obj对象) 。
This doesn't compile because it doesn't properly override public boolean equals(Object obj)
.
同样会去实现接口的方法( 1.6及以上的只有)或覆盖一个超类的方法。
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
这篇关于我们应该@Override一个接口的方法实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!