1、lambda表达式可访问外部本地final变量
final int num = 1; Converter<Integer, String> stringConverter = (from) -> String.valueOf(from + num); stringConverter.convert(2); // 3
2、与访问本地变量相反,成员变量和静态变量可以通过lambda表达式读写。这种访问变量的方式也实现在匿名变量中:
class Lambda4 { static int outerStaticNum; int outerNum; void testScopes() { Converter<Integer, String> stringConverter1 = (from) -> { outerNum = 23; return String.valueOf(from); }; Converter<Integer, String> stringConverter2 = (from) -> { outerStaticNum = 72; return String.valueOf(from); }; } }
以上是怎么用java Lambda访问变量的详细内容。更多信息请关注PHP中文网其他相关文章!