如果调用父类方法的第一条语句?第一条、语句、方法

2023-09-05 06:35:32 作者:梦境

语音识别的结果可以阅读 onActivityResult(INT申请code,INT结果code,意图数据)方法,如图所示在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() - 是你的,就全。我倾向于链的超类的第一件事情,但除非你遇到了问题,后来链应该就可以了。

java 子类覆盖了父类方法 能有什么办法调用父类的方法 一文读懂Java语言方法的重写 覆盖 Override ...

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.