简体   繁体   English

@override注释

[英]@override annotation

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. 确定toString在该方法中OvTester类覆盖toString在其超类方法。

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" OvTesterhashCode()方法必须覆盖其超类中的相同名称方法”

?

If that's not correct then what is correct? 如果这不正确那么什么是正确的?

Method overriding happens when you redefine a method with the same signature in a sublcass. 当您在子级别中重新定义具有相同签名的方法时,会发生方法重写。

So here you are overriding hashCode() , not toString() 所以在这里你重写hashCode() ,而不是toString()

The @Override annotation is optional (but a very good thing) and indicates that this is expected to be overriding. @Override注释是可选的(但这是一件非常好的事情)并且表明这应该是重写的。 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 ) 所以是的,第二个语句是真的(在这种情况下,超类是java.lang.Object

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 ". @Overrides注释表示“ 此方法会覆盖超类中具有相同名称的方法 ”。

In this case the hashCode of OvTester overrides the hashCode in Object . 在这种情况下hashCodeOvTester覆盖hashCodeObject

if its not true, then is this true: the hashCode() method in OvTester must override the same name method in its superClass? 如果它不是真的,那么这是真的: OvTesterhashCode()方法必须覆盖其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. 也许这就是你对此感到困惑的原因。

The @Override annotation does not "determine" anything. @Override注释不会“确定”任何内容。 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 . 在这种特定情况下,它只是注意到OvTester中的hashCode()实现覆盖了Object定义的hashCode()方法。 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() . 这与toString()没有任何关系,并且从hashCode()方法调用超类的toString()方法不会与覆盖toString()

is this true? 这是真的? the hashCode() method in OvTester must override the same name method in its superClass? OvTester中的hashCode()方法必须覆盖其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. 确实如此,如果超类中没有与带注释方法的签名匹配的可覆盖hashCode()方法,则注释将导致编译器引发错误。

No, it will only mean that the hashCode() method is being overridden. 不,这只会意味着重写了hashCode()方法。 The compiler will check at compile time that hashCode() really is a method (with that signature) that is being overridden. 编译器将在编译时检查hashCode()是否是一个被覆盖的方法(带有该签名)。

That code overrides the hashCode() method form the base Object class. 该代码覆盖了基础Object类的hashCode()方法。 The toString() method still has the original implementation. toString()方法仍然具有原始实现。

To override the toString() , you do the following: 要覆盖toString() ,请执行以下操作:

@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 ) 只要在子类中的方法具有相同的名称和签名作为父类中的方法, 在父类中的方法不是私人将(无论注解的存在overidden @Override

@Override is just compile time check if the implementer really overrides the method. @Override只是编译时检查实现者是否真的覆盖了该方法。

if you try overriding 如果你尝试覆盖

@Override
public void equals(Object ob){

}

it will fail to compile 它将无法编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM