从内部匿名类调用外部类的函数(与外部相同的类)

时间:2014-05-28 16:17:59

标签: java inner-classes anonymous-class

如何在不使用辅助变量的情况下调用outter类的回调函数,就像我在下面的例子中所做的那样。

请注意Calling outer class function from inner class中描述的解决方案可能无效。

public abstract class Job {
    public void callback();
}

public abstract class ExtendedJob extends Job {

    protected void handleResult() {
        // workaround for accessing the outter class
        final ExtendedJob outter = this;

        new ExtendedJob {
            public void callback() {
                // can i do the same without the outter variable?
                outter.callback();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我认为这应该可以解决问题

ExtendedJob.this.callback();
相关问题