java - 在 Java 中访问内部类的包含类

标签 java this program-flow

这就是我现在正在做的。有没有更好的方法来访问父类(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 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.

在这种情况下,您可以按照 Martijn 的建议进行操作,并使用:

workWithWidget(SearchWidget.this);

引用资料

相关问题

关于java - 在 Java 中访问内部类的包含类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2858604/

相关文章:

javascript - 将 $(this) 传递给函数

对象中的对象中的 Javascript `this`?

c# - C#抛出异常后的程序流程

java - 无法理解 Spring Boot 代码流程

java - 如何使用 TypeConverter 通过 JNA 将 C 整数映射到 Java 枚举?

java - 异常 : org. hibernate.SessionException: session 已关闭

javascript - Angular Controller As 和 babel

java - 在 Java 中按键时返回/停止执行函数

java - 合并仅用于创建或更新?

java - Json AsyncTask - 获取页面内容而不解析