@Override注释 [英] @override annotation

查看:161
本文介绍了@Override注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,如果这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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆