• Java Lamda表达式


    lambda表达式(jdk1.8新增)的一步步演化:

    package com.smile.test.lambda;
    
    public class LambdaTest {
        // step3:静态内部类
        static class B implements Study{
            @Override
            public void study(String subject) {
                System.out.println("study" + subject);
            }
        }
        public static void main(String[] args) {
            // step4:局部内部类
            class C implements Study{
                @Override
                public void study(String subject) {
                    System.out.println("study" + subject);
                }
            }
    
            A a = new A();
            a.study("java");
    
            B b = new B();
            b.study("python");
    
            C c = new C();
            c.study("C");
    
            // step5:匿名内部类
            Study study = new Study() {
                @Override
                public void study(String subject) {
                    System.out.println("study" + subject);
                }
            };
            study.study("C++");
    
            // step6:lambda表达式
            Study study1 = (String subject)->{
                System.out.println("study" + subject);
            };
            study1.study("golang");
    
            // 简化lambda表达式, 参数类型可省略,只有一个参数时,括号可省略,多个参数时,参数类型要不都写,要不都不写;
            // 方法体只有一句代码时, {}可省略,否则不能省略.
            Study study2 = subject -> System.out.println("study" + subject);
            study2.study("Math");
        }
    }
    // step1:定义一个函数式接口(接口中,有且仅有一个抽象方法)
    interface Study{
        void study(String subject);
    }
    // step2:抽象接口的实现类
    class A implements Study{
        @Override
        public void study(String subject) {
            System.out.println("study" + subject);
        }
    }
    
    studyjava
    studypython
    studyC
    studyC++
    studygolang
    studyMath
    
    Process finished with exit code 0
    

    函数式接口----可以对比Runnable接口源码:
    指内部有且仅有一个抽象方法的接口。

    package java.lang;
    
    /**
     * The <code>Runnable</code> interface should be implemented by any
     * class whose instances are intended to be executed by a thread. The
     * class must define a method of no arguments called <code>run</code>.
     * <p>
     * This interface is designed to provide a common protocol for objects that
     * wish to execute code while they are active. For example,
     * <code>Runnable</code> is implemented by class <code>Thread</code>.
     * Being active simply means that a thread has been started and has not
     * yet been stopped.
     * <p>
     * In addition, <code>Runnable</code> provides the means for a class to be
     * active while not subclassing <code>Thread</code>. A class that implements
     * <code>Runnable</code> can run without subclassing <code>Thread</code>
     * by instantiating a <code>Thread</code> instance and passing itself in
     * as the target.  In most cases, the <code>Runnable</code> interface should
     * be used if you are only planning to override the <code>run()</code>
     * method and no other <code>Thread</code> methods.
     * This is important because classes should not be subclassed
     * unless the programmer intends on modifying or enhancing the fundamental
     * behavior of the class.
     *
     * @author  Arthur van Hoff
     * @see     java.lang.Thread
     * @see     java.util.concurrent.Callable
     * @since   JDK1.0
     */
    @FunctionalInterface
    public interface Runnable {
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see     java.lang.Thread#run()
         */
        public abstract void run();
    }
    
  • 相关阅读:
    el-checkbox
    文件上传优化
    二叉树的建立
    二叉树的遍历算法
    两个二进制数多少个位(bit)不同
    二维矩阵置零
    search-a-2d-matrix(二维矩阵查找)
    Ubuntu16.04安装配置Eclipse 以及opencv的使用
    树莓派 自身摄像头的opencv调用
    树莓派3上安装Qt5
  • 原文地址:https://www.cnblogs.com/lvzl/p/14665685.html
Copyright © 2020-2023  润新知