EventHandler 何时以及为何要求对象为最终对象?
我有来自 GWT 项目的以下代码,它是 onModuleLoad() 方法的一部分(类似于 Java 的 main 方法,如果您不了解 GWT):
final TextBox t1 = new TextBox();
final Label lt1 = new Label();
t1.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
// TODO Auto-generated method stub
if (!(t1.getText().matches("\\w{2}-\\w{2}-\\w{2}")))
lt1.setText("Invalid.");
else
lt1.setText("OK.");
}
});
为什么这里的两个局部变量必须是最终的?
I have the following code from a GWT Project that is part of the onModuleLoad() method (similar to Java's main method, if you don't know GWT):
final TextBox t1 = new TextBox();
final Label lt1 = new Label();
t1.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
// TODO Auto-generated method stub
if (!(t1.getText().matches("\\w{2}-\\w{2}-\\w{2}")))
lt1.setText("Invalid.");
else
lt1.setText("OK.");
}
});
Why do the two local variables have to be final here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题是关于局部内部类访问方法的局部变量的更一般问题的专门化:方法局部内部类访问方法的局部变量(Péter Török,tnx指出了这一点)。
This question is a specialization of a more general question about local inner classes accessing local variables of the method: method local innerclasses accessing the local variables of the method (Péter Török, tnx for pointing that out).