这就是我现在正在做的。有没有更好的方法来访问父类(super class)?
public class SearchWidget {
private void addWishlistButton() {
final SearchWidget thisWidget = this;
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// A better way to access the super class?
// something like "this.super" ...?
workWithWidget(thisWidget);
}
}
}
}
我正在使用 Google Web Toolkit 进行编程,但我认为这确实是一个通用的 Java 问题。
最佳答案
您可以使用所谓的合格this
。
JLS 15.8.4. Qualified This
Any lexically enclosing instance can be referred to by explicitly qualifying the keyword
this
.Let C be the class denoted by
ClassName
. Let n be an integer such that C is the n-th lexically enclosing class of the class in which the qualified this expression appears. The value of an expression of the formClassName.this
is the n-th lexically enclosing instance ofthis
(§8.1.3). The type of the expression is C. It is a compile-time error if the current class is not an inner class of class C or C itself.
在这种情况下,您可以按照 Martijn 的建议进行操作,并使用:
workWithWidget(SearchWidget.this);
引用资料
相关问题
关于java - 在 Java 中访问内部类的包含类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2858604/