“这个"引用在构造过程中转义? [英] "this" reference escaping during construction?
本文介绍了“这个"引用在构造过程中转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我执行以下操作,
final class FooButton extends JButton{
FooButton(){
super("Foo");
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// do stuff
}
});
}
}
我是否让 this
引用隐式转义?
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
}
});
}
}
匿名ActionListener
的代码原则上可以在FooButton
对象完全初始化之前调用和使用FooButton
.
The code of the anonymous ActionListener
could in principle be called and use the FooButton
before the FooButton
object is fully initialized.
这篇关于“这个"引用在构造过程中转义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文