Java lambda表达式如何引用自身? [英] How can a Java lambda-expression reference itself?

查看:364
本文介绍了Java lambda表达式如何引用自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现本文在将旧式函数与新Java- 8个lambda函数和并行处理.我无法完全理解的一件事是对lambda函数的一个限制:从第4页开始:

I found this article to be very informative in comparison of old-style functions to new Java-8 lambda-functions and parallel processing. One thing I couldn't quite understand was one restriction on the lambda-functions: From page 4:

3.3前提条件 尽管Lambda表达式旨在提供更多的 明智的选择 工商协会 ,它们不是完整的替代品. 有几个前提条件 LambdaFicator 支票 重构之前 工商协会 变成lambda表达式.这些 前提条件是lambda表达式的固有方式 用Java实现,而不是我们工具的限制. (P1) 工商协会 必须从接口实例化.的实例 抽象或具体类不能转换为lambda 表达式. (P2) 工商协会 必须没有字段,并且只能声明一个方法. Lambda表达式表示单个匿名函数- tion;因此, 工商协会 用多种方法不能 转换为单个lambda表达式. (P3) 工商协会 不得引用 这 或者 极好的 .在 一个lambda表达式 这 和 极好的 在词法范围上, 意味着它们的解释就像它们在 封闭的环境,例如好像它们出现在州内 在lambda表达式之前[6].但是,在 工商协会 他们指的是内部阶级本身. (P4) 工商协会 不得声明递归方法.为了 执行递归调用,我们必须获取对 匿名功能.尽管 LambdaFicator 可以执行 这种重构可能会引入不必要的复杂性 进入代码并损害其可理解性.

3.3 Preconditions Although lambda expressions are intended as a more con- cise alternative to AIC , they are not a complete replacement. There are several preconditions that LambdaFicator checks before refactoring an AIC into a lambda expression. These preconditions are inherent to how lambda expressions are implemented in Java, not limitations of our tool. (P1) AIC must instantiate from an interface. Instances of abstract or concrete classes cannot be converted to lambda expressions. (P2) AIC must have no fields, and declare only one method. A lambda expression represents a single anonymous func- tion; therefore, an AIC with multiple methods can not be converted to a single lambda expression. (P3) AIC must not have references to this or super . In a lambda expression, this and super are lexically scoped, meaning they are interpreted just as they would be in the enclosing environment, e.g., as if they appeared in the state- ment before the lambda expression [6]. However, in an AIC they refer to the inner class itself. (P4) AIC must not declare a recursive method. In order to perform the recursive call, we must obtain a reference to the anonymous function. While LambdaFicator could perform this refactoring, this could introduce unneeded complexity into the code and harm understandability.

在P4上,"AIC不得声明递归方法... LambdaFicator可以执行此重构...",如何重构一个Lambda表达式以引用其自身?由于按照定义,这些lambda匿名函数没有可引用的名称,也没有对其自身的引用(上面的P3).

On P4, "AIC must not declare a recursive method... LambdaFicator could perform this refactoring...", how could one refactor a lambda expression to reference itself? Since by definition these lambda anonymous-functions don't have a name that can be referenced, and don't have a reference to themselves (P3 above).?

推荐答案

public class Test {
    static Runnable r;

    public static void main(String... args) {
        r = () -> r.run();
        r.run();
    }
}

Runnable在运行时从字段r获取对其自身的引用.

The Runnable obtains a reference to itself from the field r when it is run.

如果您不喜欢添加字段,也可以使用长度为1的数组来存储引用.

You could also use a length 1 array to store the reference if you don't like adding a field.

这篇关于Java lambda表达式如何引用自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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