java - 为什么在 java 或 android 中需要 @Override?

标签 java android

在 java 或 Android 中有@Override 注解。这是什么意思?我发现当方法来自子类或继承接口(interface)的方法时使用它,我想进一步了解,其他是 @SuppressWarnings 它也是 Anonation,如果是的话,java 使用了多少个 annonation 以及用于什么目的。

最佳答案

这里也回答了这个问题,非常简洁: Android @Override usage

It's an annotation that you can use to tell the compiler and your IDE that you intend the method that has that annotation to be an override of a super class method. They have warning/errors in case you make mistakes, for example if you intend to override a method but misspell it, if the annotation is there the IDE or the compiler will tell you that it is not in fact overriding the super class method and thus you can determine why and correct the misspelling.

This is all the more important for Android applications and activities for example, where all of the calls will be based on the activity lifecycle - and if you do not properly override the lifecycle methods they will never get called by the framework. Everything will compile fine, but your app will not work the way you intend it to. If you add the annotation, you'll get an error.

换句话说,如果您添加 @Override 这有助于您确定真的覆盖了现有方法!非常有用。

关于java - 为什么在 java 或 android 中需要 @Override?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8545540/

相关文章:

java - 部署 Maven one-jar 会部署错误的 Artifact

android - 如何在Android Studio中更改默认语言(国际非编程)

android - 如何滚动到scrollview内recyclerview中的特定位置?

java - Android 开发 - 如何通过类传递 Drawables 和 Button?

java - 将字节解码为位图

java - 使用正则表达式从字符串中获取信封ID

java - 在 GWT 之外使用 Flextable

java - 如何确定ThreadPoolTask​​Executor池和队列大小?

java - 查看 GWT HTML 源代码?

android - 在键盘打开的情况下约束布局滚动到底部