@override注解

发布于 2024-11-30 20:36:43 字数 417 浏览 2 评论 0原文

谁能告诉我这段代码是否:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

双马尾 2024-12-07 20:36:43

当您在子类中重新定义具有相同签名的方法时,就会发生方法重写。

因此,这里您要重写 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(), 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.

So yes, the 2nd statement is true (and the superclass in this case is java.lang.Object)

伴我心暖 2024-12-07 20:36:43

我想知道这是否属实,如果属实,它是如何运作的?

不,这并不完全正确。

@Overrides 注解表示“此方法会覆盖超类中同名的方法”。

在这种情况下,OvTesterhashCode 会覆盖 Object 中的 hashCode

如果不是真的,那么这是真的:OvTester 中的 hashCode() 方法必须覆盖其超类中的同名方法吗?

是的。这就是它的工作原理。


当一种方法除了调用另一种方法(几乎是您在示例中得到的方法)之外不执行任何其他操作时,它通常被称为 委托方法。也许这就是您混淆的原因。

I'd like to know if this is true, and if so how it works?

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 of OvTester overrides the hashCode in Object.

if its not true, then is this true: the hashCode() method in OvTester must override the same name method in its superClass?

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.

回忆凄美了谁 2024-12-07 20:36:43

@Override 注释不会“确定”任何内容。它只是一个标志,告诉编译器如果带注释的方法没有重写超类或接口方法,则引发错误。它是开发人员帮助保持理智的工具,仅此而已。

在这种特定情况下,只需注意 OvTester 中的 hashCode() 实现会覆盖 中定义的 hashCode() 方法。对象。这与 toString() 完全无关,并且从 hashCode() 方法调用超类的 toString() 方法不会/与重写 toString() 不同。

这是真的吗? OvTester 中的 hashCode() 方法必须重写相同的方法
在其超类中命名方法?

这确实是事实,因为如果超类中没有与带注释的方法的签名相匹配的可重写的 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 in OvTester is overriding the hashCode() method defined in Object. This has nothing at all to do with toString(), and calling the superclass's toString() method from your hashCode() method will not/is not the same thing as overriding toString().

is this true? the hashCode() method in OvTester must override the same
name method in its superClass?

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.

梦里南柯 2024-12-07 20:36:43

不,这仅意味着 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.

懷念過去 2024-12-07 20:36:43

该代码重写了 Object 基类的 hashCode() 方法。
toString() 方法仍然具有原始实现。

要重写 toString(),请执行以下操作:

@Override
public String toString() {
    //Your own toString() implememntation here
}

只要子类中的方法与父类中的方法具有相同的名称和签名,AND父类中的方法是NOT私有的,它将被覆盖(无论是否存在注释@Override

That code overrides the hashCode() method form the base Object class.
The toString() method still has the original implementation.

To override the toString(), you do the following:

@Override
public String toString() {
    //Your own toString() implememntation here
}

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)

浅唱ヾ落雨殇 2024-12-07 20:36:43

@Override 只是编译时检查实现者是否真的覆盖了该方法。

如果你尝试覆盖

@Override
public void equals(Object ob){

}

它将无法编译

@Override is just compile time check if the implementer really overrides the method.

if you try overriding

@Override
public void equals(Object ob){

}

it will fail to compile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文