代码之家  ›  专栏  ›  技术社区  ›  Bill the Lizard

如何从Java中的匿名内部类获得对封闭类的引用?[复制品]

  •  50
  • Bill the Lizard  · 技术社区  · 16 年前

    我目前正在外部类中创建一个对此的显式引用,以便在匿名内部类中有一个要引用的名称。有更好的方法吗?

    3 回复  |  直到 16 年前
        1
  •  89
  •   Frank Krueger    16 年前

    OuterClassName.this

    class Outer {
        void foo() {
            new Thread() {
                public void run() {
                    Outer.this.bar();
                }
            }.start();
        }
        void bar() {
            System.out.println("BAR!");
        }
    }
    

    Outer.this

        2
  •  19
  •   John Topley    16 年前

    EnclosingClass.this

        3
  •  1
  •   Scott Stanchfield    16 年前