Lambda 这个在 java 中的引用 [英] Lambda this reference in java

查看:43
本文介绍了Lambda 这个在 java 中的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 匿名类 转换为 lambda 表达式.但是这个匿名类我使用了 this 关键字.

I want to convert an anonymous class to a lambda expression. But this anonymous class i use the this keyword.

例如,我写了这个简单的 Observer/Observable 模式:

For exemple, i wrote this simple Observer/Observable pattern :

import java.util.ArrayList;
import java.util.Collection;

public static class Observable {
    private final Collection<Observer> notifiables = new ArrayList<>();

    public Observable() { }

    public void addObserver(Observer notifiable) { notifiables.add(notifiable); }
    public void removeObserver(Observer notifiable) { notifiables.add(notifiable); }

    public void change() {
        notifiables.forEach(notifiable -> notifiable.changed(this));
    }
}

public interface Observer {
    void changed(Observable notifier);
}

以及带有匿名类的示例代码(使用 this 关键字):

and this sample code with an anonymous class (use the this keyword) :

public class Main {

    public static void main(String[] args) {
        Observable observable = new Observable();
        observable.addObserver(new Observer() {
            @Override
            public void changed(Observable notifier) {
                notifier.removeObserver(this);
            }
        });
        observable.change();
    }
}

但是当我将其转换为 lambda 表达式时:

but when i convert it to a lambda expression :

public class Main {

    public static void main(String[] args) {
        Observable observable = new Observable();
        observable.addObserver(notifier -> { notifier.removeObserver(this); });
        observable.change();
    }
}

我收到此编译错误:

Cannot use this in a static context and in a non `static` context



public class Main {
    public void main(String[] args) {
        method();
    }

    private void method() {
        Observable observable = new Observable();
        observable.addObserver(notifier -> {
                notifier.removeObserver(this);
        });
        observable.change();
    }
}

编译错误是:

The method removeObserver(Main.Observer) in the type Main.Observable is not applicable for the arguments (Main)

所以我的问题是:有没有办法用 this 引用lambda 对象"?

So my question is : Is there a way to reference the "lambda object" with this ?

推荐答案

您不能在 lambda 表达式中引用 this.this 的语义已更改为仅从 lambda 内部引用周围类的实例.无法从 lambda 内部引用 lambda 表达式的 this.

You can't reference to this in a lambda expression. The semantic of this has been changed to reference the instance of the surrounding class only, from within the lambda. There is no way to reference to the lambda expression's this from inside the lambda.

问题在于您在 main() 方法中使用了 this.main 方法是静态的,没有对表示 this 的对象的引用.

The problem is that you use this in the main() method. The main method is static and there is no reference to an object that represents this.

当您在内部类的实例中使用 this 时,您是在引用内部类的实例.lambda 表达式不是内部类,this 不引用 lambda 表达式的实例.它引用了您在其中定义 lambda 表达式的类的实例.在您的情况下,它将是 Main 的实例.但是由于您使用的是静态方法,因此没有实例.

When you use this inside an instance of an inner class you are referencing to the instance of the inner class. A lambda expression is not an inner class, this is not referencing to the instance of the lambda expression. It is referencing to the instance of the class you define the lambda expression in. In your case it would be a instance of Main. But since your are in a static method, there is no instance.

这就是你的第二个编译错误告诉你的.您将 Main 的一个实例交给您的方法.但是你的方法签名需要一个 Observer 的实例.

This is what your second compilation error is telling you. You hand over an instance of Main to your method. But your method signature requires an instance of Observer.

更新:

Java 语言规范15.27.2 说:

与匿名类声明中出现的代码不同,名称的含义以及出现在 lambda 主体中的 this 和 super 关键字,以及引用声明的可访问性,与周围上下文中的相同(除了 lambda 参数引入新的名字).

Unlike code appearing in anonymous class declarations, the meaning of names and the this and super keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).

this(显式和隐式)在 lambda 表达式主体中的透明度 - 即,将其视为与周围上下文中的相同 - 为实现提供了更大的灵活性,并防止了正文中非限定名称的含义来自依赖于重载解析.

The transparency of this (both explicit and implicit) in the body of a lambda expression - that is, treating it the same as in the surrounding context - allows more flexibility for implementations, and prevents the meaning of unqualified names in the body from being dependent on overload resolution.

实际上,一个 lambda 表达式需要谈论自己(递归地调用自己或调用它的其他方法)是不寻常的,而更常见的是想要使用名称来引用封闭的事物否则会被隐藏的类(this, toString()).如果 lambda 表达式需要引用自身(就像通过 this 一样),则应改用方法引用或匿名内部类.

Practically speaking, it is unusual for a lambda expression to need to talk about itself (either to call itself recursively or to invoke its other methods), while it is more common to want to use names to refer to things in the enclosing class that would otherwise be shadowed (this, toString()). If it is necessary for a lambda expression to refer to itself (as if via this), a method reference or an anonymous inner class should be used instead.

这篇关于Lambda 这个在 java 中的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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