调用父类方法的目的是什么,当我们重写一个方法时?

huangapple go评论63阅读模式
英文:

What is the purpose of calling super class's method while overriding a method?

问题

在覆盖诸如AsyncTask中的onPostExecute方法时,我们可以避免或删除其中的super.onPostExecute(result);。你能告诉我为什么以及何时需要使用这个调用吗?
因为我在我的主活动中将AsyncTask实现为一个内部类

private class CustomAsyncTask extends AsyncTask<String, Void, Event> {
    @Override
    protected Event doInBackground(String... strings) {
        Event result = Utils.fetchEarthquakeData(strings[0]);
        return result;
    }

    @Override
    protected void onPostExecute(Event result) {
        super.onPostExecute(result); // 调用这个有什么效果?如果可以跳过或删除它。
        updateUi(result);
    }
}
英文:

While overriding methods like onPostExecute from AsyncTask we can avoid or remove super.onPostExecute(result); from it. Can you tell me why and when do we need to use this call?
as I here implemented AsyncTask as a inner class in my Main Activity

    private class CustomAsyncTask extends AsyncTask&lt;String, Void, Event&gt; {
    @Override
    protected Event doInBackground(String... strings) {
        Event result = Utils.fetchEarthquakeData(strings[0]);
        return result;
    }

    @Override
    protected void onPostExecute(Event result) {
        super.onPostExecute(result);// what is the effect of calling this? If it was possible to skip or remove it. 
        updateUi(result);
    }
    
}

答案1

得分: 3

当你重写一个方法时,你重新定义了这个方法,也就是说,如果在子类的实例上调用这个方法(你在子类中重写了这个方法),那么这个新版本的定义将会被调用。

当你重写一个方法(即重新定义,换句话说,用子类版本的定义替换了父类的定义)时,你有两个选择:

  1. 不使用父类定义中的任何内容,完全以一种新的方式重新编写。
  2. 在新的定义中的某个位置(开头/中间/结尾)重用父类的定义。这就像在冰淇淋蛋卷上加上樱桃,其中冰淇淋蛋卷是方法的父类定义,而樱桃则添加在新的定义中。

需要注意的是,正如Andy Turner所指出的,要求调用super被认为是一个反模式。

英文:

When you override a method, you redefine the method i.e. if this method is called on the instance of the child class (in which you have overridden the method), this new version of the definition will be called.

When you override a method (i.e. redefine or in other words, replace the superclass definition with the child's version of the definition), you have two choices:

  1. Do not use anything from the superclass definition and rewrite it in a completely new way.
  2. Reuse the superclass definition at some point (in the beginning/middle/end) in the new definition. It's like putting a cherry on the ice cream cone where ice cream cone is the superclass definition of the method and cherry is added in the new definition.

Note that as already pointed out by Andy Turner, a requirement to call super is considered an antipattern.

答案2

得分: 2

当我们覆盖某个方法时,它意味着:
1)项目中有一个接口。
2)在该接口中,有一个类是父类,另一个是子类。

现在覆盖的意思是在父类和子类中拥有相同的方法。
看下面的例子:

class Parent
{

 public Parent()
 {
  "Inside Parent Constructor";
 }
 
 public void meth()
 {
  "Inside Parent Method";
 }

}

class Child
{

 public Child()
 {
  "Inside Child Constructor";
 }
 
 public void meth()
 {
  super();              
  "Inside Parent Method";
 }

}

由于子类中使用了super关键字,它会跳转到父类的构造函数,并打印"Inside Parent Constructor"。

所以重要的是,许多时候 Android 操作系统希望访问被覆盖方法的父类,为此我们使用super()

英文:

when we override some method then it means:

  1. we have interface in our project.
  2. In that interface One Class is Parent and Other one is Child.

Now Overriding means having same method in both Parent and Child.
Se below example:

class Parent
{

 public Parent()
 {
  &quot;Inside Parent Constructor&quot;;
 }
 
 public void meth()
 {
  &quot;Inside Parent Method&quot;;
 }

}

class Child
{

 public Child()
 {
  &quot;Inside Child Constructor&quot;;
 }
 
 public void meth()
 {
  super();              
  &quot;Inside Parent Method&quot;;
 }

}

Due to super keyword in Child Class it will go to Constructor of Parent. and Print "Inside Parent Constructor".

So main thing is that many of the time Andoid OS will want to access Parent Class from which the method is overrided and for that we write super().

答案3

得分: 2

调用父类方法的目的是在重写方法时运行来自当前类继承的类中的方法,以执行该类负责的某些工作,因为这可能涉及对父类状态的一些设置。

对于您的特定情况,根据下面的问题,没有必要调用super.onPostExcecute():

https://stackoverflow.com/questions/20637355/should-i-call-super-onpostexecuteresult-in-android-asynctask

英文:

> What is the purpose of calling super class's method while overriding a method?

The purpose is to run the method in the class from which the current class inherits from to perform some work that class is responsible, as it could be some setup of the super class' state.

For your particular case, there is no need to call super.onPostExcecute() as stated in the question below:

https://stackoverflow.com/questions/20637355/should-i-call-super-onpostexecuteresult-in-android-asynctask

huangapple
  • 本文由 发表于 2020年8月15日 18:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63425140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定