Java 我们应该@Override 接口的方法实现吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/212614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should we @Override an interface's method implementation?
提问by Benno Richters
Should a method that implements an interface method be annotated with @Override
?
实现接口方法的方法应该用 注释@Override
吗?
The javadoc of the Override
annotationsays:
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?
我不认为接口在技术上是超类。或者是吗?
采纳答案by jjnguy
You should use @Override whenever possible. It prevents simple mistakes from being made. Example:
您应该尽可能使用@Override。它可以防止出现简单的错误。例子:
class C {
@Override
public boolean equals(SomeClass obj){
// code ...
}
}
This doesn't compile because it doesn't properly override public boolean equals(Object obj)
.
这不会编译,因为它没有正确覆盖public boolean equals(Object obj)
.
The same will go for methods that implement an interface (1.6 and above only) or override a Super class's method.
实现接口(仅限 1.6 及更高版本)或覆盖超类方法的方法也是如此。
回答by Alex B
I would use it at every opportunity. See When do you use Java's @Override annotation and why?
我会一有机会就使用它。请参阅何时使用 Java 的 @Override 注释以及为什么?
回答by Jon Skeet
I believe that javac behaviour has changed - with 1.5 it prohibited the annotation, with 1.6 it doesn't. The annotation provides an extra compile-time check, so if you're using 1.6 I'd go for it.
我相信 javac 的行为已经改变 - 1.5 禁止注释,1.6 没有。注释提供了额外的编译时检查,因此如果您使用的是 1.6,我会使用它。
回答by Arne Burmeister
Overriding your own methods inherited from your own classes will typically not break on refactorings using an ide. But if you override a method inherited from a library it is recommended to use it. If you dont, you will often get no error on a later library change, but a well hidden bug.
覆盖从您自己的类继承的您自己的方法通常不会在使用 ide 进行重构时中断。但是,如果您覆盖从库继承的方法,则建议使用它。如果你不这样做,你通常不会在以后的库更改中得到错误,而是一个隐藏得很好的错误。
回答by savetheclocktower
Eclipse itself will add the @Override
annotation when you tell it to "generate unimplemented methods" during creation of a class that implements an interface.
@Override
当您在创建实现接口的类期间告诉它“生成未实现的方法”时,Eclipse 本身将添加注释。
回答by Fabian Steeg
For me, often times this is the only reason some code requires Java 6 to compile. Not sure if it's worth it.
对我来说,这通常是某些代码需要 Java 6 才能编译的唯一原因。不确定是否值得。
回答by Silent Warrior
JDK 5.0 does not allow you to use @Override
annotation if you are implementing method declared in interface (its compilation error), but JDK 6.0 allows it. So may be you can configure your project preference according to your requirement.
@Override
如果您正在实现接口中声明的方法(其编译错误),则JDK 5.0 不允许您使用注释,但 JDK 6.0 允许使用。因此,您可以根据您的要求配置您的项目偏好。
回答by lwpro2
It's not a problem with JDK. In Eclipse Helios, it allows the @Override annotation for implemented interface methods, whichever JDK 5 or 6. As for Eclipse Galileo, the @Override annotation is not allowed, whichever JDK 5 or 6.
这不是JDK的问题。在 Eclipse Helios 中,它允许对已实现的接口方法使用 @Override 注释,无论是 JDK 5 还是 6。至于 Eclipse Galileo,不允许使用 @Override 注释,无论是 JDK 5 还是 6。
回答by GKelly
You should always annotate methods with @Override
if it's available.
@Override
如果可用,您应该始终注释方法。
In JDK 5 this means overriding methods of superclasses, in JDK 6, and 7 it means overriding methods of superclasses, and implementing methods of interfaces. The reason, as mentioned previously, is it allows the compiler to catch errors where you think you are overriding (or implementing) a method, but are actually defining a new method (different signature).
在 JDK 5 中,这意味着覆盖超类的方法,在 JDK 6 和 7 中,它意味着覆盖超类的方法和实现接口的方法。如前所述,原因是它允许编译器在您认为您覆盖(或实现)一个方法,但实际上定义了一个新方法(不同的签名)时捕获错误。
The equals(Object)
vs. equals(YourObject)
example is a standard case in point, but the same argument can be made for interface implementations.
所述equals(Object)
对比equals(YourObject)
例子就是一个标准的情况下,但相同的参数可以用于接口实现制成。
I'd imagine the reason it's not mandatory to annotate implementing methods of interfaces is that JDK 5 flagged this as a compile error. If JDK 6 made this annotation mandatory, it would break backwards compatibility.
我认为不强制注释接口的实现方法的原因是 JDK 5 将此标记为编译错误。如果 JDK 6 强制使用此注释,则会破坏向后兼容性。
I am not an Eclipse user, but in other IDEs (IntelliJ), the @Override
annotation is only added when implementing interface methods if the project is set as a JDK 6+ project. I would imagine that Eclipse is similar.
我不是Eclipse用户,但在其他IDE(IntelliJ)中,@Override
如果项目设置为JDK 6+项目,则仅在实现接口方法时添加注释。我会想象 Eclipse 是相似的。
However, I would have preferred to see a different annotation for this usage, maybe an @Implements
annotation.
但是,我更希望看到这种用法的不同注释,也许是@Implements
注释。
回答by spujia
The problem with including the @Override
is that it makes you think that you forgot to call the super.theOverridenMethod()
method, which is very confusing. This should be crystal-clear. Perhaps Java should offer an @Interface
to be used here. Oh well, yet another half-assed Java peculiarity...
包含 的问题@Override
在于它让您认为您忘记调用该super.theOverridenMethod()
方法,这非常令人困惑。这应该是非常清楚的。也许Java应该提供一个@Interface
在这里使用。哦,还有另一个半途而废的 Java 特性......