你什么时候使用 Java 的 @Override 注释,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/94361/
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
When do you use Java's @Override annotation and why?
提问by Alex B
What are the best practices for using Java's @Override
annotation and why?
使用 Java@Override
注释的最佳实践是什么?为什么?
It seems like it would be overkill to mark every single overridden method with the @Override
annotation. Are there certain programming situations that call for using the @Override
and others that should never use the @Override
?
用@Override
注释标记每个被覆盖的方法似乎有点矫枉过正。是否有某些编程情况需要使用 ,@Override
而其他情况则不应使用@Override
?
采纳答案by Dave L.
Use it every time you override a method for two benefits. Do it so that you can take advantage of the compiler checking to make sure you actually are overriding a method when you think you are. This way, if you make a common mistake of misspelling a method name or not correctly matching the parameters, you will be warned that you method does not actually override as you think it does. Secondly, it makes your code easier to understand because it is more obvious when methods are overwritten.
每次覆盖一个方法时使用它有两个好处。这样做以便您可以利用编译器检查来确保您确实在您认为自己覆盖了某个方法时覆盖了该方法。这样,如果您犯了错误拼写方法名称或未正确匹配参数的常见错误,您将被警告您的方法实际上并没有像您认为的那样覆盖。其次,它使您的代码更容易理解,因为当方法被覆盖时更明显。
Additionally, in Java 1.6 you can use it to mark when a method implements an interface for the same benefits. I think it would be better to have a separate annotation (like @Implements
), but it's better than nothing.
此外,在 Java 1.6 中,您可以使用它来标记方法何时实现接口以获得相同的好处。我认为有一个单独的注释(如@Implements
)会更好,但总比没有好。
回答by Hank Gay
I use it every time. It's more information that I can use to quickly figure out what is going on when I revisit the code in a year and I've forgotten what I was thinking the first time.
我每次都用它。当我在一年内重新访问代码并且我已经忘记了我第一次在想什么时,我可以使用更多信息来快速弄清楚发生了什么。
回答by jjnguy
I always use the tag. It is a simple compile-time flag to catch little mistakes that I might make.
我总是使用标签。这是一个简单的编译时标志,用于捕捉我可能犯的小错误。
It will catch things like tostring()
instead of toString()
它会捕获类似的东西tostring()
而不是toString()
The little things help in large projects.
小事情有助于大项目。
回答by Greg Mattes
Whenever a method overrides another method, or a method implements a signature in an interface.
每当一个方法覆盖另一个方法,或者一个方法在接口中实现一个签名。
The @Override
annotation assures you that you did in fact override something. Without the annotation you risk a misspelling or a difference in parameter types and number.
该@Override
注解向你保证,你实际上覆盖的东西一样。如果没有注释,您可能会面临拼写错误或参数类型和数量不同的风险。
回答by JeeBee
It does allow you (well, the compiler) to catch when you've used the wrong spelling on a method name you are overriding.
它确实允许您(好吧,编译器)在您对要覆盖的方法名称使用错误拼写时进行捕获。
回答by toluju
Using the @Override
annotation acts as a compile-time safeguard against a common programming mistake. It will throw a compilation error if you have the annotation on a method you're not actually overriding the superclass method.
使用@Override
注释作为编译时保护措施,防止常见的编程错误。如果您在方法上有注释,您实际上并未覆盖超类方法,则会引发编译错误。
The most common case where this is useful is when you are changing a method in the base class to have a different parameter list. A method in a subclass that used to override the superclass method will no longer do so due the changed method signature. This can sometimes cause strange and unexpected behavior, especially when dealing with complex inheritance structures. The @Override
annotation safeguards against this.
这很有用的最常见情况是当您更改基类中的方法以具有不同的参数列表时。由于更改了方法签名,子类中用于覆盖超类方法的方法将不再这样做。这有时会导致奇怪和意外的行为,尤其是在处理复杂的继承结构时。该@Override
注释保障反对这一点。
回答by Tom Hawtin - tackline
If you find yourself overriding (non-abstract) methods very often, you probably want to take a look at your design. It is very useful when the compiler would not otherwise catch the error. For instance trying to override initValue() in ThreadLocal, which I have done.
如果您发现自己经常覆盖(非抽象)方法,您可能想看看您的设计。当编译器不会以其他方式捕获错误时,它非常有用。例如,尝试在 ThreadLocal 中覆盖 initValue(),我已经完成了。
Using @Override when implementing interface methods (1.6+ feature) seems a bit overkill for me. If you have loads of methods some of which override and some don't, that probably bad design again (and your editor will probably show which is which if you don't know).
在实现接口方法(1.6+ 功能)时使用 @Override 对我来说似乎有点矫枉过正。如果您有大量方法,其中一些方法会被覆盖,而另一些不会,那可能又是糟糕的设计(如果您不知道,您的编辑器可能会显示哪个是哪个)。
回答by Tom Hawtin - tackline
The best practive is to always use it (or have the IDE fill them for you)
最好的做法是始终使用它(或让 IDE 为您填写)
@Override usefulness is to detect changes in parent classes which has not been reported down the hierarchy. Without it, you can change a method signature and forget to alter its overrides, with @Override, the compiler will catch it for you.
@Override 的用处是检测尚未在层次结构中报告的父类中的更改。没有它,您可以更改方法签名而忘记更改其覆盖,使用@Override,编译器会为您捕获它。
That kind of safety net is always good to have.
拥有这种安全网总是好的。
回答by jon
I think it is most useful as a compile-time reminder that the intention of the method is to override a parent method. As an example:
我认为它作为编译时提醒最有用,该方法的意图是覆盖父方法。举个例子:
protected boolean displaySensitiveInformation() {
return false;
}
You will often see something like the above method that overrides a method in the base class. This is an important implementation detail of this class -- we don't want sensitive information to be displayed.
您经常会看到类似上述方法的内容覆盖了基类中的方法。这是该类的一个重要实现细节——我们不希望显示敏感信息。
Suppose this method is changed in the parent class to
假设这个方法在父类中改成
protected boolean displaySensitiveInformation(Context context) {
return true;
}
This change will not cause any compile time errors or warnings - but it completely changes the intended behavior of the subclass.
此更改不会导致任何编译时错误或警告 - 但它会完全更改子类的预期行为。
To answer your question: you should use the @Override annotation if the lack of a method with the same signature in a superclass is indicative of a bug.
回答您的问题:如果超类中缺少具有相同签名的方法表明存在错误,您应该使用 @Override 注释。
回答by Asgeir S. Nilsen
@Override on interfaces actually are helpful, because you will get warnings if you change the interface.
@Override 在接口上实际上是有帮助的,因为如果您更改接口,您将收到警告。