Android的@覆盖使用 [英] Android @Override usage

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

问题描述

(新手到爪哇,旧时代的C#的家伙。)

我已经注意到了很多的Andr​​oid例如code使用 @覆盖的。我认为所有的Java方法是默认为虚拟?

那么,什么呢 @覆盖吗?

例如:

 私有类HelloWebViewClient扩展WebViewClient {

    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
        view.loadUrl(URL);
        返回true;
    }
}
 

解决方案

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

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

(Newbie to Java, old time C# guy.)

I have noticed a lot of the use of @Override in Android example code. I thought that all Java methods were by default "Virtual"?

What then does @Override do?

Example:

private class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

解决方案

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.

这篇关于Android的@覆盖使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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