Lambda 表达式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    // 使用匿名内部类
    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello from thread (Override)");
        }
    }).start();

    // 使用 lambda 表达式
    new Thread(
        () -> System.out.println("Hello from thread (lambda)")
    ).start();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    // 使用匿名内部类
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("The button was clicked (Override)");
        }
    });

    // 使用 lambda 表达式
    button.addActionListener( (e) -> {
        System.out.println("The button was clicked (lambda)");
    });
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计