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();
    }    
    
  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/freeht/p/13034597.html
Copyright © 2011-2022 走看看