为什么@覆盖需要在Java或者Android? [英] Why @Override needed in java or android?

查看:146
本文介绍了为什么@覆盖需要在Java或者Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的Java或者Android有@Override注解。这是什么意思?我发现它还是,我想进一步了解等时使用的方法是从子类继承接口的方法@燮pressWarnings它也Anonation,如果是多少annonation Java和为此目的使用。

In java or Android there are @Override annotations. What does it mean? I found that it is used when method is from subclass or inherited interface's method, I want to know further and other is @SuppressWarnings its also Anonation, if yes how many annonation used by java and for which purpose.

推荐答案

这个问题也是在这里得到解答,而且相当简洁: 的Andr​​oid @覆盖使用

This question is also answered here, and quite succinctly: Android @Override usage

这是,你可以用它来告诉你打算拥有的注释是一个超类方法的覆盖的方法,编译器和你的IDE注释。他们在情况下,你会犯错误,比如警告/错误,如果你想覆盖的方法,但拼错了,如果注释是那里的IDE或编译器会告诉你,这不是事实覆盖超类的方法,因而你可以决定的原因和纠正拼写错误。

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.

这是更加重要的Andr​​oid应用程序和活动,例如,所有的通话将基于活动的生命周期 - 如果你没有正确重写生命周期方法,他们将永远不会被调用的框架。一切都将编译好的,但你的应用程序将无法正常工作你想让它的方式。如果添加了注解,你会得到一个错误。

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.

在换句话说,如果你添加@覆盖这可以帮助你做出的确定的你的真正的覆盖现有的方法! pretty的织补有用的。

In other words, if you add @Override this helps you make sure you are really overriding an existing method! Pretty darn useful.

这篇关于为什么@覆盖需要在Java或者Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆