与Object-C块相比,Java匿名类的限制 [英] Limitations of Java Anonymous Classes compared to Objective-C Blocks

查看:180
本文介绍了与Object-C块相比,Java匿名类的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中发现块之后,我开始围绕一阶函数和闭包。 Java是另一种语言,我听说关闭(或缺乏),以及匿名类如何弥补这一点。

I'm just starting to wrap my head around first order functions and closures after discovering blocks in Objective-C. Java is another language where I've heard about closures (or lack thereof) and how anonymous classes make up for this somewhat.

我可以肯定地看到closures的优点在Objective-C中的块,但是匿名Java类的限制是什么?

I can definitely see the advantages of closures as blocks in Objective-C, but what are the limitations of anonymous Java classes? To what extent do they 'somewhat' make up for the lack of true closures?

推荐答案

Java匿名类是真的,真的很冗长,它们在某种程度上弥补了缺少真正的闭包。除了需要定义它们的大量模板外,Java的一些设计决策意味着许多常见任务比其他语言更加冗长。例如,将可变上限导入闭包是Java中的屁股的痛苦。

Java anonymous classes are really, really wordy. Apart from the vast amounts of boilerplate that you need just to define them, some of Java's design decisions mean that a lot of common tasks are much more verbose than in other languages. For example, importing mutable upvalues into the closure is a pain in the arse in Java.

基本上,Java不支持upvalues;而是通过将它们(通过值)通过不可见的参数传递给类的构造函数来模拟它们。因为它们是通过值传递的,所以在类中修改它们不会影响构造类的方法中的副本,因此编译器会将它们声明为final以避免混淆。例如:

Basically, Java doesn't support upvalues; instead they're simulated by passing them (by value) into the class via invisible parameters to the class's constructor. Because they're passed by value, modifying them inside the class won't affect the copy in the method that constructed the class, so the compiler makes you declare them final to avoid confusing yourself. e.g.:

Runnable function()
{
   final int i = 4;
   return new Runnable()
   {
       public void run()
       {
            System.out.println("i="+i);
            // can't modify i here
       }
   }
}


b $ b

在你需要修改变量的情况下,例如在几乎所有情况下,闭包都会有用,你必须作弊:

On occasions where you do need to modify the variable, for example in pretty much every case where closures would be useful, you have to cheat:

Runnable function()
{
   final int[] i = new int[1];
   i[0] = 4;
   return new Runnable()
   {
       public void run()
       {
            System.out.println("i="+i[0]);
            i[0] = i[0] + 1;
       }
   }
}

c> i 本身仍然是不可变的,但因为它指向一个可变对象,我可以改变对象的内容。 (自然地,在现实生活中,我会使用一个类而不是一个数组,因为使用数组是非常丑陋的,这使得它甚至更多 wordy。)

Here, i itself is still immutable, but because it points at a mutable object, I can change the contents of the object. (Naturally, in real life I'd use a class rather than an array, because using arrays is really ugly. And that makes it even more wordy.)

我认为下一个Java版本会有语法糖,使所有这一切更容易,但现在以闭包为中心的编程在Java是相当繁琐。我发现通常更容易改变逻辑不使用闭包,只是让我保持使用的代码量足够小,以便于理解。

I gather that the next Java release is going to have syntactic sugar to make all this easier, but right now closure-centric programming is pretty cumbersome in Java. I find it's frequently easier to change the logic not to use closures, simply to allow me to keep the amount of code in use small enough to be comprehensible.

这篇关于与Object-C块相比,Java匿名类的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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