Java - “@Override”有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4185397/
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
Java - what is "@Override" used for?
提问by JDelage
Possible Duplicate:
What's “@Override” there for in java?
可能的重复:
java 中的“@Override”是什么?
I've never put "@Override" before a method until now. I see some code examples with it, but I don't understand its utility. I'd love some explanation.
直到现在,我从未在方法之前放置“@Override”。我看到一些带有它的代码示例,但我不明白它的效用。我想要一些解释。
Many thanks,
非常感谢,
JDelage
JDelage
采纳答案by Andreas Dolk
First, you can't annotate a classwith @Override
. This annotation indicates that a method declaration is intended to override a method declaration in a superclass.
首先,你不能注释一个类有@Override
。此注释表明方法声明旨在覆盖超类中的方法声明。
You don't have to annotate overriding methods butif you use this annotation and your annotated method does notoverride a superclass method, then the compiler will generate an error message.
您不必注释覆盖方法,但是如果您使用此注释并且您的注释方法没有覆盖超类方法,那么编译器将生成错误消息。
回答by Jeremy
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.
指示方法声明旨在覆盖超类中的方法声明。如果一个方法使用此注解类型进行注解但没有覆盖超类方法,则编译器需要生成错误消息。
http://download.oracle.com/javase/6/docs/api/java/lang/Override.html
http://download.oracle.com/javase/6/docs/api/java/lang/Override.html
The case I like to explain its use is when overriding equals
.
我想解释它的用法的情况是当覆盖equals
.
This will error because equals
expects an Object
parameter:
这会出错,因为equals
需要一个Object
参数:
public class Foo{
@Override
public boolean equals(Foo f){
return true;
}
}
回答by Andrzej Doyle
The best example - overriding equals()
.
最好的例子 - 覆盖equals()
。
If you write a class like this:
如果你写一个这样的类:
public class Foo
{
public String bar;
public boolean equals(Foo other)
{
return this.bar.equals(other.bar);
}
}
then you've overloadedthe equals method, rather than overriding Object.equals
as was intended.
那么您已经重载了 equals 方法,而不是Object.equals
按预期覆盖。
If you annotate the equals method with @Override
, the compiler will give you an error stating (correctly) that you haven't overridden a superclass method.
如果您使用 注释 equals 方法@Override
,编译器会给您一个错误,说明(正确地)您没有覆盖超类方法。
In Java 6, you can use this for implementing interface methods too - this is handy when you're only adding a method to your class to satisfy some interface, and hence the compiler can check that it's required and alert you to the interface changing.
在 Java 6 中,您也可以使用它来实现接口方法——当您只向类添加一个方法来满足某些接口时,这很方便,因此编译器可以检查它是否是必需的,并在接口更改时提醒您。
As with all annotations it's effectively a programmatic comment, but having the compiler check that your assumptions are (still) correct is very handy in these cases.
与所有注释一样,它实际上是一个编程注释,但是在这些情况下,让编译器检查您的假设(仍然)正确是非常方便的。
回答by Nathan Hughes
It's there to express that you expect the method to be overriding a superclass method. It does come in handy when you make a mistake spelling the method name or give it the wrong parameters so that it does not override what you thought it was overriding.
它表示您希望该方法覆盖超类方法。当您在拼写方法名称时出错或给它错误的参数时,它确实会派上用场,这样它就不会覆盖您认为要覆盖的内容。
回答by Mikhail
It's a conventional comment. Some compilers make sure that the function followed by @Override is actually an override... just a failsafe
这是一个传统的评论。一些编译器确保@Override 后面的函数实际上是一个覆盖......只是一个故障安全