“这个”施工期间参考逃逸? [英] "this" reference escaping during construction?
本文介绍了“这个”施工期间参考逃逸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我执行以下操作,
final class FooButton extends JButton {
FooButton(){
super(Foo);
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// do stuff
}
}
}
}
是的,因为在匿名内部类中,你可以像这样访问它。 :
final class FooButton extends JButton {
Foo(){
super(Foo);
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
FooButton button = FooButton.this;
// ... do有按钮的东西
}
});
}
}
匿名在
并使用 FooButton
对象完全初始化之前,原则上可以调用ActionListener FooButton
。
If I do the following,
final class FooButton extends JButton{
FooButton(){
super("Foo");
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// do stuff
}
});
}
}
am I letting the this
reference implicitly escape?
解决方案
Yes, because in the anonymous inner class you could access it like this:
final class FooButton extends JButton {
Foo() {
super("Foo");
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FooButton button = FooButton.this;
// ... do something with the button
}
});
}
}
The code of the anonymous ActionListener
could in principle be called and use the FooButton
before the FooButton
object is fully initialized.
这篇关于“这个”施工期间参考逃逸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文