@Override注释 [英] @override annotation
问题描述
谁能告诉我,如果这code:
Can anyone tell me if this code:
public class OvTester {
@Override
public int hashCode() {
return toString().hashCode();
}
}
确定在 OvTester
类的<code>的toString 方法重写的toString
方法,其超类。
determines that the toString
method in the OvTester
class overrides the toString
method in its superclass.
我想知道,如果这是真的,如果是它是如何工作?
I'd like to know if this is true, and if so how it works?
如果这不是真的,那么这是真的:
If it's not true, then is this true:
的的散code()
方法 OvTester
必须覆盖同名方法在其超的
"the hashCode()
method in OvTester
must override the same name method in its superclass"
如果这是不正确那么什么是正确的?
If that's not correct then what is correct?
推荐答案
当你重新定义一个方法,在sublcass相同签名的方法压倒一切的发生。
Method overriding happens when you redefine a method with the same signature in a sublcass.
所以在这里你将覆盖散code()
,而不是的toString()
So here you are overriding hashCode()
, not toString()
的 @覆盖
注释是可选的(但一个非常好的事情),并表示这是有望被改写。如果你拼错的东西或者有错误类型的参数,编译器会发出警告。
The @Override
annotation is optional (but a very good thing) and indicates that this is expected to be overriding. If you misspell something or have a wrongly-typed parameter, the compiler will warn you.
所以,是的,第二个说法是正确的(在这种情况下的超类是的java.lang.Object
)
So yes, the 2nd statement is true (and the superclass in this case is java.lang.Object
)
这篇关于@Override注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!