如何在匿名内部类中使用外部方法的输入? [英] How to use Outer Method's input in Anonymous Inner Class?

查看:210
本文介绍了如何在匿名内部类中使用外部方法的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Instance如何在我的Anonymou内部类中使用输入'hasTypedSomeToken' -

For Instance how can I use the input 'hasTypedSomeToken' in my Anonymou inner class in the following -

    public class Login {

        void display(boolean hasTypedSomeToken)
        {
           //some code here

               Button btnLogIn = new Button("Login", new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    if(Login.this.hasTypedSomeToken) //HOW TO USE hasTypedSomeToken HERE 
                    {

                    //do something

                    }
                }
          }
      }


推荐答案

方法中声明的变量是局部变量。例如 hasTypedSomeToken btnLogIn 显示方法中的局部变量。

The variables declared within a method are local variables. e.g. hasTypedSomeToken and btnLogIn are local variables in your display method.

如果你想使用本地内部类中的那些变量(在方法中定义的类,例如在您的情况下实现 ClickHandler 的匿名类)然后您必须声明它们 final

And if you want to use those variables inside a local inner class (classes that are defined inside a method e.g. the anonymous class that implements ClickHandler in your case) then you have to declare them final.

例如

void display(final boolean hasTypedSomeToken) {

如果你看 Login.this.hasTypedSomeToken 这个用于访问成员变量。局部变量不是类的成员。它们是仅在方法中存在的自动变量。

If you look at Login.this.hasTypedSomeToken, this is used to access member variables. Local variables are not members of class. They are automatic variables that live only within the method.

这篇关于如何在匿名内部类中使用外部方法的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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