Java:我应该在实现抽象方法时添加@Override注释吗?

时间:2023-02-05 12:46:50

When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?

在Java中覆盖非虚方法时,建议使用@Override注释,但是如果实现抽象方法怎么办?我也应该使用@Override吗?

4 个解决方案

#1


56  

I tend to prefer the use of @Override in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).

在这种情况下,我更倾向于使用@Override,因此如果超类更改(要么完全删除方法,要么更改其签名等),方法会在子类中被标记。

The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.

唯一真正的区别在于,如果没有注释,如果超类/接口中的方法被更改或删除,则所讨论的实现只是成为该类的“正常”方法。因此,如果您仅为履行合同而实施该方法,则应添加注释;如果方法在你的类中有意义,你可能不应该添加它,无论任何实现的接口或继承的抽象方法。

#2


20  

Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"

是的 - 再一次,它告诉编译器,“我真的想在这里覆盖一个方法。如果没有相应的方法来覆盖,我犯了一个错误,想要被告知它!”

Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.

就我个人而言,我觉得很遗憾这只是一个注释而不是语言的一部分(就像它在C#中一样),但这当然是后见之明的好处。

#3


13  

Yes. It is recommended practise by Joshua Bloch in Effective Java.

是。 Joshua Bloch建议在Effective Java中练习。

#4


6  

Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override annotation because the compiler would give an error anyway. However, "it is not harmful to do so".

实际上,Joshua Bloch在Effective Java(第2版)的第178页的最后一段中说,对于覆盖抽象方法的具体类的方法来说,使用Override注释并不是必不可少的,因为编译器无论如何都会给出错误。但是,“这样做无害”。

I'd recommend choosing a strategy and sticking with it consistently.

我建议选择一种策略并坚持不懈地坚持下去。

#1


56  

I tend to prefer the use of @Override in this case, so that the method gets flagged in the subclasses if the superclass changes (either removing the method altogether, or changing its signature, etc.).

在这种情况下,我更倾向于使用@Override,因此如果超类更改(要么完全删除方法,要么更改其签名等),方法会在子类中被标记。

The only real difference is that without the annotation, if the method in the superclass/interface is changed or removed, the implementation in question simply becomes a "normal" method of that class. Thus you should add the annotation if you're implementing the method solely to fulfil the contract; and you probably shouldn't add it if the method makes sense in your class regardless of any implemented interfaces or inherited abstract methods.

唯一真正的区别在于,如果没有注释,如果超类/接口中的方法被更改或删除,则所讨论的实现只是成为该类的“正常”方法。因此,如果您仅为履行合同而实施该方法,则应添加注释;如果方法在你的类中有意义,你可能不应该添加它,无论任何实现的接口或继承的抽象方法。

#2


20  

Yes - again, it tells the compiler, "I really want to be overriding a method here. If there isn't a corresponding method to override, I've made a mistake and want to be told about it!"

是的 - 再一次,它告诉编译器,“我真的想在这里覆盖一个方法。如果没有相应的方法来覆盖,我犯了一个错误,想要被告知它!”

Personally I think it's a pity that this is just an annotation rather than part of the language (as it is in C#) but that's the benefit of hindsight, of course.

就我个人而言,我觉得很遗憾这只是一个注释而不是语言的一部分(就像它在C#中一样),但这当然是后见之明的好处。

#3


13  

Yes. It is recommended practise by Joshua Bloch in Effective Java.

是。 Joshua Bloch建议在Effective Java中练习。

#4


6  

Actually, Joshua Bloch, in the final paragraph of page 178 in Effective Java (2nd Ed.), says that it's not essential for methods of concrete classes that override abstract methods to use the Override annotation because the compiler would give an error anyway. However, "it is not harmful to do so".

实际上,Joshua Bloch在Effective Java(第2版)的第178页的最后一段中说,对于覆盖抽象方法的具体类的方法来说,使用Override注释并不是必不可少的,因为编译器无论如何都会给出错误。但是,“这样做无害”。

I'd recommend choosing a strategy and sticking with it consistently.

我建议选择一种策略并坚持不懈地坚持下去。