方法中与其参数引用之一相同的引用指的是参数是否正确?
A reference within a method that is identical to one of its parameters reference refers to the parameter correct?
Towards the end of this section 在 lambda 表达式的 oracle 教程中,它似乎在说 x
指的是方法的封闭 class 中的成员,但是由于方法有一个与 x
相同的参数引用,我认为它指的是局部参数而不是封闭 class.
的成员
For example, the lambda expression directly accesses the parameter x of the method methodInFirstLevel. To access variables in the enclosing class, use the keyword this. In this example, this.x refers to the member variable FirstLevel.x.
但是教程似乎立即自相矛盾:
However, like local and anonymous classes, a lambda expression can only access local variables and parameters of the enclosing block that are final or effectively final. For example, suppose that you add the following assignment statement immediately after the methodInFirstLevel definition statement:
void methodInFirstLevel(int x) {
x = 99;
// ...
}
Because of this assignment statement, the variable FirstLevel.x is not effectively final anymore.
什么?该示例方法中的赋值是 x =
而不是 this.x =
那么该赋值对 FirstLevel.x
有何影响?
根据我收集到的信息,教程说示例 lambda 函数中的 x = 99
是 而不是 访问 class 成员变量。它试图修改函数参数,该参数必须是最终的,因此,这是一个无效操作。 x = 99
会产生错误。
后面的教程说影响了成员变量,FirstLevel.x
。这是不正确的,因为 x = 99
只能引用函数参数。另一方面,this.x
会引用 FirstLevel.x
.
一般情况下,传递给函数的是引用。
然后在函数内为局部变量分配此引用。它通常不会对函数外部的变量造成任何更改,除非您使用某种形式的全局变量造成这种情况。
名称的重合不会导致任何问题,因为它们指向不同的内存位置。
Towards the end of this section 在 lambda 表达式的 oracle 教程中,它似乎在说 x
指的是方法的封闭 class 中的成员,但是由于方法有一个与 x
相同的参数引用,我认为它指的是局部参数而不是封闭 class.
For example, the lambda expression directly accesses the parameter x of the method methodInFirstLevel. To access variables in the enclosing class, use the keyword this. In this example, this.x refers to the member variable FirstLevel.x.
但是教程似乎立即自相矛盾:
However, like local and anonymous classes, a lambda expression can only access local variables and parameters of the enclosing block that are final or effectively final. For example, suppose that you add the following assignment statement immediately after the methodInFirstLevel definition statement:
void methodInFirstLevel(int x) {
x = 99;
// ...
}
Because of this assignment statement, the variable FirstLevel.x is not effectively final anymore.
什么?该示例方法中的赋值是 x =
而不是 this.x =
那么该赋值对 FirstLevel.x
有何影响?
根据我收集到的信息,教程说示例 lambda 函数中的 x = 99
是 而不是 访问 class 成员变量。它试图修改函数参数,该参数必须是最终的,因此,这是一个无效操作。 x = 99
会产生错误。
后面的教程说影响了成员变量,FirstLevel.x
。这是不正确的,因为 x = 99
只能引用函数参数。另一方面,this.x
会引用 FirstLevel.x
.
一般情况下,传递给函数的是引用。 然后在函数内为局部变量分配此引用。它通常不会对函数外部的变量造成任何更改,除非您使用某种形式的全局变量造成这种情况。 名称的重合不会导致任何问题,因为它们指向不同的内存位置。