简体   繁体   English

从nester内部类访问外部内部类

[英]Accessing outer inner class from nester inner class

I have the following code: 我有以下代码:

public class Bar {}
public class FooBar {}

public class Foo {

    public void method() {
        new Bar() {

            void otherMethod() { }

            void barMethod() {

                new FooBar() {

                    void fooBarMethod() {
                        Bar.this.otherMethod(); // not compiles
                    }   
                };
            }
        };
    }

}

So i have an anonymous inner class, which has another anonymous inner class in it. 所以我有一个匿名内部类,其中有另一个匿名内部类。 The question: is there any way to access the methods of the outer inner class Bar from the inner inner class FooBar ? 问题:有没有办法从内部内部类FooBar访问外部内部类Bar的方法?

You can call the method directly using simple name: 您可以使用简单名称直接调用该方法:

void fooBarMethod() {
    otherMethod(); // compiles
}

This will fail, the moment you define another method with the name otherMethod() in the new FooBar() anonymous class. 当您在new FooBar()匿名类中使用名称otherMethod()定义另一个方法时,这将失败。

Bar.this wouldn't really work, because that's an anonymous class there, whose name is given at compile time. Bar.this不会真正起作用,因为那是一个匿名类,其名称在编译时给出。 It will get a name like Foo$1 . 它会得到像Foo$1这样的名字。 So, no you can't have something like Bar.this . 所以,不,你不能有像Bar.this这样的东西。


Ok, I've written this source file: 好的,我写了这个源文件:

class Bar { }

class FooBar { }

public class Demo {

    public static void main() {
        new Demo().method();
    }

    public void method() {
        new Bar() {

            void otherMethod() { System.out.println("Hello"); }

            void barMethod() {

                new FooBar() {

                    void fooBarMethod() {
                        otherMethod(); // not compiles
                    }   
                }.fooBarMethod();
            }
        }.barMethod();
    }
}

The class files generated would be: 生成的类文件将是:

Bar.class
FooBar.class
Demo.class

Demo$1.class    // For `new Bar()` anonymous class
Demo$1$1.class  // For `new FooBar()` anonymous class

Now, let's go straight to the byte code of new FooBar() anonymous class. 现在,让我们直接进入new FooBar()匿名类的字节代码。 The class will be named - Demo$1$1 . 该课程将被命名为 - Demo$1$1 So, running the javap command, I get this output: 所以,运行javap命令,我得到这个输出:

class Demo$1$1 extends FooBar {
  final Demo$1 this$1;

  Demo$1$1(Demo$1);
    Code:
       0: aload_0
       1: aload_1
       2: putfield      #1                  // Field this$1:LDemo$1;
       5: aload_0
       6: invokespecial #2                  // Method FooBar."<init>":()V
       9: return

  void fooBarMethod();
    Code:
       0: aload_0
       1: getfield      #1                  // Field this$1:LDemo$1;
       4: invokevirtual #3                  // Method Demo$1.otherMethod:()V
       7: return
}

The final field there is a copy of reference to new Bar() instance. final字段有一个对new Bar()实例的引用副本。 So, the otherMethod() is invoked on this$1 reference, which is a reference to the instance of new Bar() anonymous inner class. 因此,在this$1引用上调用了otherMethod() ,它引用了new Bar()匿名内部类的实例。 Well, you were trying to do that only, but since that's an anonymous inner class, you cannot access the this reference directly. 好吧,你只是试图这样做,但由于这是一个匿名的内部类,你不能直接访问this引用。 But, that is implicit there. 但是,这隐含在那里。


For more detailed analysis: 有关更详细的分析:

If you name them then they aren't anonymous inner classes, they are just inner classes. 如果你命名它们,那么它们不是匿名的内部类,它们只是内部类。 If you don't name them then you can't call them explicitly, although you can call them if there is no name conflict. 如果您没有为它们命名,那么您无法明确地调用它们,但如果没有名称冲突,您可以调用它们。

Your code is completely invalid though, you are creating a new Bar but never defining a class called Bar. 您的代码完全无效,但您正在创建一个新的Bar但从未定义一个名为Bar的类。

Forget the line you say "not compiles" on, the first "new Bar()" doesn't compile... 忘记你说“不编译”的行,第一个“新的Bar()”不编译......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM