我有这样的代码:
TextBox txt = new TextBox(){
public void onLoad(){
this.addFocusHandler(new FocusHandler(){
//some codes here
//if I use "this" keyword, it refers to the handler, but how can I get a reference to the textbox?
});
}
};
问题嵌入位置。
编辑:
关于答案,创建预定义引用适用于这种情况,但这显然失去了(或至少减少了)匿名对象/函数的好处。
希望能找到一种不用创建新引用的方法。而只是从该范围获取引用。
所有的答案之后,这里是一个结论:
- 反射在 GWT 中不起作用。 (至少我没有成功)
obj.getClass()
有效,但其他类似getMethods()
或getEnclosingClass()
无效. - 获取引用的方式可以是在正确范围内声明引用,也可以是获取更高级别的对象引用和向下引用。我更喜欢后者,因为您不需要创建新变量。
最佳答案
TextBox txt = new TextBox(){
public void onLoad(){
final TextBox finalThis = this;
this.addFocusHandler(new FocusHandler(){
finalThis.doSomething();
);
}
};
关于java - 如何获取对匿名对象的引用 2 级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12516339/