gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:48 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com