zoukankan      html  css  js  c++  java
  • java8新特性-lambda(变量捕获)

    1.匿名内部类中的变量捕获

    public class App {
      String s1 = "全局变量";
      public void testInnerClass() {
        String s2 = "局部变量";
        new Thread(new Runnable() {
          String s3 = "内部变量";
          @Override
          public void run() {
            System.out.println(this.s3);    //this关键字,表示的是当前内部类类型的对象
            System.out.println(s1);     //直接访问全局变量
                 System.out.println(s2);     //直接访问局部变量,但是不能对局部变量进行修改,默认是final类型
              }
        }).start();
      }
    
      public static void main(String[] args) {
        App app = new App();
        app.testInnerClass();
      }
    }

    2.lambda表达式中的变量捕获

    public void testLambda() {
        String s2 = "局部变量Lambda";
        new Thread(() -> {
            String s3 = "内部变量Lambda";
             //访问全局变量
             System.out.println(this.s1);    //this关键字,表示就是所属方法所在类型对对象
             //访问局部变量
             System.out.println(s2);     //不能局部修改,默认是final类型
             System.out.println(s3);
        }).start();
    }    
    
  • 相关阅读:
    linux 中rz sz 文件传输
    linux find 命令
    深度学习的博客
    cifar10数据的转换、代码解释
    gflags的使用实例(转载)
    leveldb使用 (转载)
    (转载+整理)Leveldb安装及例子
    2013-09-25-【随笔】-Roy
    2013-09-22 [随笔]-Roy
    2013-08-12【随笔2】-Roy
  • 原文地址:https://www.cnblogs.com/freeht/p/13034597.html
Copyright © 2011-2022 走看看