变量已在方法 lambda 中定义 [英] Variable is already defined in method lambda

查看:22
本文介绍了变量已在方法 lambda 中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下几乎可编译的 Java 8 代码:

Consider the following almost compilable Java 8 code:

public static void main(String[] args) {

    LinkedList<User> users = null;
    users.add(new User(1, "User1"));
    users.add(new User(2, "User2"));
    users.add(new User(3, "User3"));

    User user = users.stream().filter((user) -> user.getId() == 1).findAny().get();
}

static class User {

    int id;
    String username;

    public User() {
    }

    public User(int id, String username) {
        this.id = id;
        this.username = username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public int getId() {
        return id;
    }
}

你会注意到 User user = users.stream().filter((user) -> user.getId() == 1).findAny().get(); throws编译器错误:

You'll notice User user = users.stream().filter((user) -> user.getId() == 1).findAny().get(); throws a compiler error:

变量 user 已经在方法 main(String[]) 中定义

variable user is already defined in method main(String[])

我的问题是:为什么 Lambda 表达式将正在初始化的变量与已经定义的 Lambda 表达式考虑在同一行?我了解 Lambda 会在自身外部寻找(并使用)局部变量,因此您不能将在 Lambda 内部使用的变量命名为与外部变量相同的名称.但是为什么正在定义的变量被认为已经定义了?

My question is: Why do Lambda expressions consider the variable that is being initialized on the same line as the Lambda expression as already defined? I understand Lambdas look outside themselves for (and use) local variables, so you can't name the variables you use inside the Lambda the same as an outside variable. But why is the variable that is being defined considered already defined?

推荐答案

让我们转到 名称及其范围

方法的形参范围(第 8.4.1 节),构造函数(第 8.8.1 节)或 lambda 表达式(第 15.27 节)是方法、构造函数或 lambda 表达式.

The scope of a formal parameter of a method (§8.4.1), constructor (§8.8.1), or lambda expression (§15.27) is the entire body of the method, constructor, or lambda expression.

块中局部变量声明的范围(第 14.4 节)是声明出现的块的其余部分,以它的开头自己的初始化器 并在右侧包含任何进一步的声明符局部变量声明语句.

The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

然后,关于阴影和遮蔽

一个局部变量(第 14.4 节),形式参数(第 8.4.1 节,第 15.27.1 节),异常参数(第 14.20 节)和本地类(第 14.3 节)只能是使用简单名称而不是限定名称(第 6.2 节)引用.

A local variable (§14.4), formal parameter (§8.4.1, §15.27.1), exception parameter (§14.20), and local class (§14.3) can only be referred to using a simple name, not a qualified name (§6.2).

某些声明在局部范围内是不允许的变量、形式参数、异常参数或本地类声明,因为无法区分仅使用简单名称声明的实体.

Some declarations are not permitted within the scope of a local variable, formal parameter, exception parameter, or local class declaration because it would be impossible to distinguish between the declared entities using only simple names.

如果使用了局部变量 v 的名称,则是编译时错误在 v 的范围内声明一个新的变量,除非新的变量在一个类中声明,该类的声明在诉范围

所以,在

User user = users.stream().filter((user) -> user.getId() == 1).findAny().get();

,变量 user 的作用域是该块中它之后的所有内容.现在您正在尝试使用该变量的名称在范围内声明一个新变量,但不是

, the scope of the variable user is everything after it in that block. Now you are trying to use the name of that variable to declare a new variable within the scope, but not

在声明在 v 范围内的类中.

所以出现编译时错误.(它是在 lambda 表达式中声明的,而不是在类中.)

so a compile time error occurs. (It's declared in a lambda expression, not in a class.)

这篇关于变量已在方法 lambda 中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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