Java中匿名内部类内部访问外部类实例
可能的重复:
来自匿名内部类的外部类的关键字?< /a>
我需要访问匿名内部类中外部类的实例并执行类似的操作。谁能澄清这是否正确?
public class ClassA{
ClassA refernceOfClassA = this;
public void m(){
//Do something
}
Runnable target = new Runnable(){
public void run(){
//Code goes here using the refernceOfClassA
refernceOfClassA.m();
}
};
}
Possible Duplicate:
keyword for the outer class from an anonymous inner class?
I need to access the instance of the outer class within the anonymous inner class and did something like this. Can anyone clarify whether this is correct or not?
public class ClassA{
ClassA refernceOfClassA = this;
public void m(){
//Do something
}
Runnable target = new Runnable(){
public void run(){
//Code goes here using the refernceOfClassA
refernceOfClassA.m();
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够从内部类调用方法
m()
。You should be able to just call the method
m()
from the inner class.我不相信你甚至需要referenceOfClassA。您只需访问外部类的属性并正常调用其方法即可。
I do not believe you even need the referenceOfClassA. You can just access properties on the outer class and call its methods normally.