如果调用父类方法的第一条语句? [英] Should the call to the superclass method be the first statement?

查看:175
本文介绍了如果调用父类方法的第一条语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语音识别的结果可以阅读 onActivityResult(INT申请code,INT结果code,意图数据)方法,如图所示在<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html">this例如的。该方法覆盖类相同的方法活动:为什么是调用父类的方法不是第一条语句

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == VOICE_RECOGNITION_REQUEST_ code和;&安培;结果code == RESULT_OK){
        //填充列表视图中的字符串识别器认为它可能已经听说
        // ...
    }

    super.onActivityResult(要求code,因此code,数据);
}
 

解决方案

方法,你会覆盖组件创建的一部分(的onCreate() ONSTART() onResume()等),你应该链超作为第一个发言,以确保Android有其偶然性你试图做一些事情,依赖于已经做过的工作前要做的工作。

方法,你会覆盖组件的破坏(的一部分的onPause()的onStop()的onDestroy()等),你应该先和链做你的工作,以超类的最后一件事。这样一来,万一安卓清理东西,你的工作取决于,你会先对你的工作。

这是返回的东西以外无效方法 onCreateOptionsMenu()等),有时你链的超在return语句,假设你是不是专门做的事情,需要强制一个特定的返回值。

其他的一切 - 如 onActivityResult() - 是你的,就全。我倾向于链的超类的第一件事情,但除非你遇到了问题,后来链应该就可以了。

The results of a speech recognition can be read in the onActivityResult(int requestCode, int resultCode, Intent data) method, as shown in this example. This method overrides the same method in class Activity: why is the call to the superclass method not the first statement?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        // ...
    }

    super.onActivityResult(requestCode, resultCode, data);
}

解决方案

Methods you override that are part of component creation (onCreate(), onStart(), onResume(), etc.), you should chain to the superclass as the first statement, to ensure that Android has its chance to do its work before you attempt to do something that relies upon that work having been done.

Methods you override that are part of component destruction (onPause(), onStop(), onDestroy(), etc.), you should do your work first and chain to the superclass as the last thing. That way, in case Android cleans up something that your work depends upon, you will have done your work first.

Methods that return something other than void (onCreateOptionsMenu(), etc.), sometimes you chain to the superclass in the return statement, assuming that you are not specifically doing something that needs to force a particular return value.

Everything else -- such as onActivityResult() -- is up to you, on the whole. I tend to chain to the superclass as the first thing, but unless you are running into problems, chaining later should be fine.

这篇关于如果调用父类方法的第一条语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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