【问题标题】:Calling to the owner class from a listener从侦听器调用所有者类
【发布时间】:2012-06-13 17:46:12
【问题描述】:

有跟随课-

public class GUIclass1 extends org.eclipse.swt.widgets.Composite {
    private void initGUI() {

        {
            // The setting of the open file button.
            openButton = new Button(this, SWT.PUSH | SWT.CENTER);
            openButton.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent evt) {
                    foo() ; 
                }
            });
        }
    }

    public void foo() {
        // implementation ..
    }
}

正如您在 addSelectionListener 中看到的,有一个对方法 foo() 的调用。

我的问题是 - 我应该将哪个引用写为 foo() 的前缀,以便知道 foo() 与哪个类相关。

我尝试了super().foo(),但没有成功。

【问题讨论】:

    标签: java button methods swt listener


    【解决方案1】:

    你可以称它为GUIclass1.this.foo()

    【讨论】:

      【解决方案2】:

      试试这个,

      我们知道an inner class has an implicit access to the members of the outer class, 所以this.foo() Will NOT work,但是

      GUIclass1.this.foo() Will WORK.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        • 2012-04-30
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 2019-07-25
        相关资源
        最近更新 更多