怎么可能“这个”从内部类访问外部类? [英] How can "this" of the outer class be accessed from an inner class?

查看:102
本文介绍了怎么可能“这个”从内部类访问外部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Java内部类中获取 this 的引用?

Is it possible to get a reference to this from within a Java inner class?

ie

class Outer {

  void aMethod() {

    NewClass newClass = new NewClass() {
      void bMethod() {
        // How to I get access to "this" (pointing to outer) from here?
      }
    };
  }
}


推荐答案

你可以像这样访问外部类的实例:

You can access the instance of the outer class like this:

Outer.this

这篇关于怎么可能“这个”从内部类访问外部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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