我们可以在Java的lambda表达式中访问哪种变量?
lambda表达式由两个部分组成,一个是参数 ,另一种是一个表达 ,并且这两个部件都用箭头分离(->)符号。Lambda表达式可以访问其封闭 范围的变量。
Lambda表达式访问这两个实例 和静态 的变量,它是包类,也可以访问本地 变量 它们是有效的最终或最后。
语法
( argument-list ) -> expression
示例
interface TestInterface {
void print();
}
public class LambdaExpressionTest {
int a; // instance variable static int b; // static variable LambdaExpressionTest(int x) { // constructor to initialise instance variable this.a = x;
}
void show() {
// lambda expression to define print() method TestInterface testInterface = () -> { // accessing of instance and static variable using lambda expression System.out.println("Value of a is: "+ a);
System.out.println("Value of b is: "+ b);
};
testInterface.print();
}
public static void main(String arg[]) {
LambdaExpressionTest test = new LambdaExpressionTest(10);
test.show();
}
}输出结果
Value of a is: 10 Value of b is: 0
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短