简体   繁体   English

在我的例子中,从子类中调用父抽象类中的函数

[英]Invoke function in parent abstract class from child class in my case

I have a abstract class: 我有一个抽象类:

public abstract class Parent{

    public void cook(){
        DoSomething(); //call abstract method
    }

    protected abstract void DoSomething();

}

I have a concrete class which imiplements the above abstract class: 我有一个具体的类,它上面提到了上面的抽象类:

public Child extends Parent{
   private Toy toy;

   public void initToy(){
      toy.setOnPlayListener(new OnPlayListener() {
            @Override
            public void onPlay() {
                //How to call parent class cook() method here?
            }
        });
   }

   @Override
   public void DoSomething(){...}
}

I want to call the cook() method of Parent class under current Child instance in the override onPlay() function of OnPlayListener() in Child class. 我想在Child类的OnPlayListener()的覆盖onPlay()函数中OnPlayListener() 当前Child实例下Parent类的cook()方法。 How to do it? 怎么做?

========Update======= ========更新=======

Thanks for your answers, now I would like to make it clear, are Child.super.cook() & Child.this.cook() the same thing ?? 谢谢你的回答,现在我想说清楚, Child.super.cook()Child.this.cook()是一回事吗?

You need to get the enclosing class instance's super class 您需要获取封闭类实例的超类

@Override
public void onPlay() {
    Child.super.cook();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM