我们在编写Java代码时真的需要@Override等吗?
我想知道在我们要覆盖的代码前面添加 @Override
的功能是什么。无论有没有它,我都做过,似乎一切都很顺利(至少对我来说)。
Possible Duplicate:
When do you use Java's @Override annotation and why?
I wonder what the functionality of adding @Override
in front of the code we would like to override is. I have done with and without it, and it seemed that everything was just going well (at least, for me).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这不是必要,但强烈建议这样做。它可以防止你搬起石头砸自己的脚。当您编写一个您认为会覆盖另一个函数但您拼写错误的函数并导致完全意外的行为时,它有助于防止出现这种情况。
It is not necessary, but it is highly recommended. It keeps you from shooting yourself in the foot. It helps prevent the case when you write a function that you think overrides another one but you misspelled something and you get completely unexpected behavior.
当您说(通过注释)指定的方法应该覆盖超类方法(或在 Java 6 或更高版本中实现接口方法)时,它可以让编译器为您进行双重检查。如果该方法实际上没有重写超类方法(或实现接口方法),则编译器会将其标记为错误。这通常表明您的方法名称有拼写错误或方法签名有误。
需要它吗? 绝对不需要,但它是一种便宜的方式,可以
......并且当你的 IDE 帮助你包含它时甚至更便宜......
It lets the compiler double-check for you when you say (by annotating) that a specified method is supposed to override a superclass method (or implement an interface method in Java 6 or later). If the method does not, in fact, override a superclass method (or implement an interface method), the compiler will flag this as an error. This often indicates that you have a typo in the method name or made a mistake in the method signature.
Need it? Absolutely not, but its such a cheap way to
... and even cheaper when your IDE is helping you include it ...
推荐这样做,因为它有助于管理一致性。想象一下有人会更改超类方法的名称(并且只有在那里,而不在依赖于它的类中执行名称更改,这是完全假设的:)),那么由于编译错误,您将是第一个知道的人。
It's recommended since it helps to manage consistence. Imagine someone will change the name of superclass method (and only there, without performing name-changes in classes depending on it which is quite hypothetical :) ), then you will be the first one to know due to compilation errors.
唯一的区别是,如果你用 @Override 注释一个方法并且你没有覆盖任何东西,编译器会抱怨。
检查什么时候使用javas重写注释以及为什么?
The only difference would be that if you annotate a method with @Override and you are not overriding anything, the compiler will complaint.
Check When do you use javas override annotation and why?
它多次帮助我避免方法名称中的拼写错误/大小写差异。如果没有它,它就是这些恼人的错误之一,可能需要很长时间才能找到。
它不会增加任何功能差异,但告诉编译器您认为您正在重写一个方法,这样它就可以抱怨,如果您没有,这在您搞砸了某个地方时非常有用!
It's helped me a good couple of times to avoid spelling errors / case differences in method names. Without it it's one of these annoying bugs that can take ages to track down.
It doesn't add any functional difference, but telling the compiler you think you're overriding a method so it can complain if you're not is very useful when you've screwed up somewhere!
强烈建议使用它,因为它可以提高您的代码的不稳定性并帮助其他人维护您的代码。
It's highly recommended to use it since it increase undersatadability of ur code and help others to maintain ur code too .
就我个人而言,我认为除了进行编译时错误检查的 IDE 之外,它没有什么用处,但这是我的观点。原因:假设你
在运行时,你可能会调用 B.methodName() - B.methodName() 是否覆盖类 A() 的同名对 B 来说并不重要。对于类 B 来说,超类 (A) 是否实现 methodName() 甚至都不重要。我的意思是继承是一条单向街 - 你不能通过使用 @override 来取消继承某些东西 - 编译器可以检查的只是超类中是否存在具有相同签名的方法 - 无论如何都不会使用该方法。
对于其他答案,如果有人编辑 A.java 来删除或重命名 methodName() 或更改其签名,您仍然可以毫无问题地调用 B.methodName(),但前提是您不使用该 @override。我认为这也是它不成为Java语言一部分的原因。
Personally I dont think it is useful except for IDE's that do the compile-time error checking, but that is my opinion. Reason: suppose you have
At runtime, you will probably call B.methodName() - it doesnt matter to B whether B.methodName() overrides the same name of class A(). To class B, it shouldn't even matter if the superclass (A) implements methodName(). What I mean is that inheritance is a one-way street - You cannot un-inherit something by using a @override - all the compiler can check is if there is a method with the same signature in the superclass - which will not be used anyways.
For the other answers, if someone edits A.java to delete or rename methodName() or change its signature, you can still call B.methodName() without a problem, but only if you do not use that @override. I think this is also why it is not a part of the Java language.
不!
如果您发现它有用,那么您需要一个真正的 IDE,或者学习如何使用它。
NO!
If you find it useful, you need a real IDE, or learn how to use it.