本文介绍了来自匿名内部类的外部类的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码段中:
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();
}
}
);
}
}
有没有关键字从内部类引用外部类?基本上我想做的是 outer.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.
推荐答案
通常,您使用 OuterClassName.this
来引用外部类的封闭实例.
In general you use OuterClassName.this
to refer to the enclosing instance of the outer class.
在你的例子中,这将是 a.this.otherMethod()
In your example that would be a.this.otherMethod()
这篇关于来自匿名内部类的外部类的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!