在Java中访问内部类的包含类 [英] Accessing the containing class of an inner class in Java

查看:139
本文介绍了在Java中访问内部类的包含类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我现在正在做的事情。有没有更好的方法来访问超类?

  public class SearchWidget {
private void addWishlistButton(){
final SearchWidget thisWidget = this;
button.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event){
//访问超类的更好方法?
//像这样的东西.super...?
workWithWidget(thisWidget);
}
}
}
}

我正在使用Google Web Toolkit进行编程,但我认为这确实是一个普通的Java问题。

解决方案

您可以使用所谓的合格



JLS 15.8.4。合格的这个




任何词汇封闭的实例都可以通过显式限定关键字 this 来引用。 / p>

C 成为 ClassName 表示的类。让 n 是一个整体这样 C n - 词类封闭的类,其中出现限定的表达式。 ClassName.this 形式的表达式的值是 n - 词汇封闭的实例(§8.1.3)。表达式的类型是 C 。如果当前类不是类 C C 本身的内部类,则为编译时错误。


在这种情况下,你可以做Martijn的建议,并使用:

  workWithWidget( SearchWidget.this); 



参考文献





相关问题




This is what I'm doing now. Is there a better way to access the 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);
            }
        }
    }
}

I'm programming with Google Web Toolkit, but I think this is really a generic Java question.

解决方案

You can use what is called the qualified 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 form ClassName.this is the n-th lexically enclosing instance of this (§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.

In this case, you can do what Martijn suggests, and use:

workWithWidget(SearchWidget.this);

References

Related questions

这篇关于在Java中访问内部类的包含类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆