本文介绍了在抽象类中,“ this”是关键字引用父类或子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类Flight。 Flight包含方法schedule(),该方法调用私有方法schedule(最终的Flight f)

I have an abstract class Flight. Flight contains the method schedule() which calls the private method schedule(final Flight f)

public void schedule()
{
    schedule(this);
}

private void schedule(final Flight f)
{
    new Timer().schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            f.checkIn();
            updateList();
        }
    }, this.getDate());
}

现在让我们说我有一类可以扩展飞行的SouthWestFlight

Lets now say I have a class SouthWestFlight that extends Flight

Flight f = new SouthWestFlight(); //ignore the missing params doesn't matter for example
f.schedule();

是否可以将Flight实例或SouthWestFlight实例作为schedule方法中的参数传递?

Would this pass the instance of the Flight or the instance of SouthWestFlight as a parameter in the schedule method?

推荐答案

Flight 的实例正是 SouthWestFlight 的实例。您没有有两个不同的实例。您的SoutWestFlight实例 IS A 飞行。实际上,您不能因为 Flight 实例本身而使其抽象。

The instance of Flight it's exactly the instance of SouthWestFlight. You don't have 2 different instances. Your SoutWestFlight instance IS A Flight. Actually you can't have a Flight instance itself cause it's abstract.

这篇关于在抽象类中,“ this”是关键字引用父类或子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:53