1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-25 02:27:10 +08:00

Update Lambda表达式.md

This commit is contained in:
haiqiang 2019-03-10 22:58:23 +08:00 committed by GitHub
parent 36147197d7
commit 38e568cbe5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,6 +78,7 @@ public class VaraibleHide {
};
inner.printInt(30);
inner = (s) -> {
//Variable used in lambda expression should be final or effectively final
//!int x = 10;
//!x= 50; error
System.out.print(x);
@ -85,4 +86,10 @@ public class VaraibleHide {
inner.printInt(30);
}
}
输出 :
30
20
```
lambda表达式和内部类一样对外部变量捕获时外部变量必须为final或者是最终变量(effectively final)的,也就是说这个变量初始化后就不能为它赋新值,
同时lambda不像内部类/匿名类lambda表达式与外围嵌套块有着相同的作用域因此对变量命名的有关规则对lambda同样适用。
## 5.[方法引用]()