从内部类调用外部类函数 [英] Calling outer class function from inner class

查看:30
本文介绍了从内部类调用外部类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中实现了嵌套类,需要从内部类调用外部类方法.

I have implemented a nested class in Java, and I need to call the outer class method from the inner class.

class Outer {
    void show() {
        System.out.println("outter show");
    }

    class Inner{
        void show() {
            System.out.println("inner show");
        }
    }
}

如何调用Outer 方法show?

推荐答案

您需要在外部类的调用前加上前缀:

You need to prefix the call by the outer class:

Outer.this.show();

这篇关于从内部类调用外部类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆