@override注解
谁能告诉我这段代码是否:
public class OvTester {
@Override
public int hashCode() {
return toString().hashCode();
}
}
确定 OvTester
类中的 toString
方法覆盖其超类中的 toString
方法。
我想知道这是否属实,如果属实,它是如何运作的?
如果不是真的,那么这是真的:
“OvTester 中的 hashCode()
方法必须覆盖其超类中的同名方法”
?
如果这不正确,那么什么才是正确的呢?
Can anyone tell me if this code:
public class OvTester {
@Override
public int hashCode() {
return toString().hashCode();
}
}
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:
"the hashCode()
method in OvTester
must override the same name method in its superclass"
?
If that's not correct then what is correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您在子类中重新定义具有相同签名的方法时,就会发生方法重写。
因此,这里您要重写
hashCode()
,而不是toString()
。@Override
注释是可选的(但非常好),它表明预计这将是压倒一切的。如果您拼写错误或参数输入错误,编译器会警告您。所以是的,第二条语句是正确的(本例中的超类是 java.lang.Object )
Method overriding happens when you redefine a method with the same signature in a sublcass.
So here you are overriding
hashCode()
, nottoString()
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.So yes, the 2nd statement is true (and the superclass in this case is
java.lang.Object
)不,这并不完全正确。
@Overrides
注解表示“此方法会覆盖超类中同名的方法”。在这种情况下,
OvTester
的hashCode
会覆盖Object
中的hashCode
。是的。这就是它的工作原理。
当一种方法除了调用另一种方法(几乎是您在示例中得到的方法)之外不执行任何其他操作时,它通常被称为 委托方法。也许这就是您混淆的原因。
No, it's not exactly true.
The
@Overrides
annotation says that "this method overrides the method with the same name in the super class".In this case the
hashCode
ofOvTester
overrides thehashCode
inObject
.Yes. That's exactly how it works.
When one method doesn't do anything else than to call another method (almost what you got in your example) it is usually referred to as a delegate method. Perhaps that's what you're confusing this with.
@Override
注释不会“确定”任何内容。它只是一个标志,告诉编译器如果带注释的方法没有重写超类或接口方法,则引发错误。它是开发人员帮助保持理智的工具,仅此而已。在这种特定情况下,只需注意
OvTester
中的hashCode()
实现会覆盖中定义的
。这与hashCode()
方法。对象toString()
完全无关,并且从hashCode()
方法调用超类的toString()
方法不会/与重写 toString() 不同。这确实是事实,因为如果超类中没有与带注释的方法的签名相匹配的可重写的
hashCode()
方法,注释将导致编译器引发错误。The
@Override
annotation does not "determine" anything. It is simply a flag that tells the compiler to raise an error if the annotated method is not overriding a superclass or interface method. It's a tool for developers to help maintain their sanity, and nothing more.In this specific case, it is simply noting that the
hashCode()
implementation inOvTester
is overriding thehashCode()
method defined inObject
. This has nothing at all to do withtoString()
, and calling the superclass'stoString()
method from yourhashCode()
method will not/is not the same thing as overridingtoString()
.That is indeed true, in the sense that the annotation will cause the compiler to raise an error if there is not an overridable
hashCode()
method in the superclass that matches the signature of the annotated method.不,这仅意味着 hashCode() 方法被重写。编译器将在编译时检查 hashCode() 是否确实是一个被重写的方法(具有该签名)。
No, it will only mean that the hashCode() method is being overridden. The compiler will check at compile time that hashCode() really is a method (with that signature) that is being overridden.
该代码重写了
Object
基类的hashCode()
方法。toString()
方法仍然具有原始实现。要重写
toString()
,请执行以下操作:只要子类中的方法与父类中的方法具有相同的名称和签名,AND父类中的方法是NOT私有的,它将被覆盖(无论是否存在注释
@Override
)That code overrides the
hashCode()
method form the baseObject
class.The
toString()
method still has the original implementation.To override the
toString()
, you do the following:As long as the method in the child class has the same name and signature as the method in the parent class, AND the method in the parent class is NOT private it will be overidden(regardless of the presence of the annotation
@Override
)@Override
只是编译时检查实现者是否真的覆盖了该方法。如果你尝试覆盖
它将无法编译
@Override
is just compile time check if the implementer really overrides the method.if you try overriding
it will fail to compile