Keyword for the outer class from an anonymous inner class [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:36:54

问题


In the following snippet:

public class a {
    public void otherMethod(){}
    public void doStuff(String str, InnerClass b){}
    public void method(a){
        doStuff(\"asd\",
            new InnerClass(){
                public void innerMethod(){
                    otherMethod();
                }
            }
        );
    }
}

Is there a keyword to refer to the outer class from the inner class? Basically what I want to do is outer.otherMethod(), or something of the like, but can\'t seem to find anything.


回答1:


In general you use OuterClassName.this to refer to the enclosing instance of the outer class.

In your example that would be a.this.otherMethod()




回答2:


OuterClassName.this.outerClassMethod();


来源:https://stackoverflow.com/questions/56974/keyword-for-the-outer-class-from-an-anonymous-inner-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!