为什么匿名类中只能使用最终变量? [英] Why are only final variables accessible in anonymous class?

查看:164
本文介绍了为什么匿名类中只能使用最终变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. a 只能在这里。为什么?如何在 onClick()方法中重新分配 a 而不保留为私人成员?

  1. a can only be final here. Why? How can I reassign a in onClick() method without keeping it as private member?

private void f(Button b, final int a){
    b.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            int b = a*5;

        }
    });
}


  • 如何返回 5 *一个当它点击?我的意思是,

  • How can I return the 5 * a when it clicked? I mean,

    private void f(Button b, final int a){
        b.addClickHandler(new ClickHandler() {
    
            @Override
            public void onClick(ClickEvent event) {
                 int b = a*5;
                 return b; // but return type is void 
            }
        });
    }
    



  • 推荐答案

    如注释所述,其中一些在Java 8中变得无关紧要,其中 final 可以是隐式的。

    As noted in comments, some of this becomes irrelevant in Java 8, where final can be implicit. Only an effectively final variable can be used in an anonymous inner class or lambda expression though.

    只有一个有效最终变量可以用于匿名内部类或lambda表达式>基本上是由于Java管理关闭的方式。

    It's basically due to the way Java manages closures.

    当您创建匿名内部类的实例时,该类中使用的任何变量都会通过自动生成的构造函数复制其。这避免了编译器必须自动生成各种额外的类型来保存局部变量的逻辑状态,例如C#编译器...(当C#在匿名函数中捕获变量时,它确实捕获变量 - 关闭可以以方法的主体看到的方式更新变量,反之亦然。)

    When you create an instance of an anonymous inner class, any variables which are used within that class have their values copied in via the autogenerated constructor. This avoids the compiler having to autogenerate various extra types to hold the logical state of the "local variables", as for example the C# compiler does... (When C# captures a variable in an anonymous function, it really captures the variable - the closure can update the variable in a way which is seen by the main body of the method, and vice versa.)

    由于该值已被复制到匿名内部类,如果该变量可以被该方法的其余部分修改,则看起来很奇怪 - 您可以使用似乎使用过期变量的代码(因为这将有效地 >发生...你会在不同的时间使用一个副本)。同样,如果您可以在匿名内部类中进行更改,开发人员可能会希望这些更改在封闭方法的主体内可见。

    As the value has been copied into the instance of the anonymous inner class, it would look odd if the variable could be modified by the rest of the method - you could have code which appeared to be working with an out-of-date variable (because that's effectively what would be happening... you'd be working with a copy taken at a different time). Likewise if you could make changes within the anonymous inner class, developers might expect those changes to be visible within the body of the enclosing method.

    使变量final取消所有这些可能性 - 由于价值根本无法改变,您无需担心这些更改是否可见。允许方法和匿名内部类看到对方的变化的唯一方法是使用可变类型的一些描述。这可能是封闭类本身,一个数组,一个可变的包装类型...这样的东西。基本上,这有点像在一种方法之间进行通信:一种方法的参数所做的更改不会被其调用者看到,而是对对象所做的更改通过参数可以看到。

    Making the variable final removes all these possibilities - as the value can't be changed at all, you don't need to worry about whether such changes will be visible. The only ways to allow the method and the anonymous inner class see each other's changes is to use a mutable type of some description. This could be the enclosing class itself, an array, a mutable wrapper type... anything like that. Basically it's a bit like communicating between one method and another: changes made to the parameters of one method aren't seen by its caller, but changes made to the objects referred to by the parameters are seen.

    如果您对Java和C#闭包之间的更详细比较感兴趣,我有一个文章进一步介绍。我想在这个答案中专注于Java方面:)

    If you're interested in a more detailed comparison between Java and C# closures, I have an article which goes into it further. I wanted to focus on the Java side in this answer :)

    这篇关于为什么匿名类中只能使用最终变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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