Lambda表达式

 

先看个小示例:

package ..lambda.test01;

/**
 * @author : 
 */
public interface TestInterface {
    void testMethod();
}

class MyClass implements TestInterface{
    @Override
    public void testMethod() {
        System.out.println("重新实现了testMethod方法。。");
    }
}

class Demo{
    public static void main(String[] args) {
        //创建接口对应的实现类的对象:
        TestInterface t = new MyClass();
        t.testMethod();
        
        

        TestInterface T = new TestInterface() {
            @Override
            public void testMethod() {
                System.out.println("重写了testMethod方法,匿名内部类中的实现");
            }
        };
        T.testMethod();

        
        TestInterface t = () -> System.out.println("这个是Lambda表达式的实现方式");
        t.testMethod();

        
        
        
        打印:重新实现了testMethod方法。。
              重写了testMethod方法,匿名内部类中的实现
              这个是Lambda表达式的实现方式
        

        /*
        总结:
        1.Lambda表达式是一个新的语法结构
        2.语法层面简洁了
        3.Lambda表达式的本质就是:接口的实现类的具体的对象
        4.应用场合:复用性没有那么强的时候
        5.语法:
        -> 箭头操作符  Lambda操作符
        ->左侧:  Lambda的形参列表 ---等效于  对应的接口的那个抽象方法的 形参列表
        ->右侧: 其实就是抽象方法的方法体
         */
    }
}

 

语法结构1:无参,无返回值:

package ..lambda.test02;

/**
 * @author : -
 * 语法结构1:无参,无返回值
 */
public interface TestInterface {
    void testMethod();
}

class Test01{
    //这是main方法,程序的入口
    public static void main(String[] args) {

        TestInterface ttt = new TestInterface() {
            @Override
            public void testMethod() {
                System.out.println("ttt");
            }
        };
        ttt.testMethod();


        TestInterface tt = () -> {
            System.out.println("tt");
        };
        tt.testMethod();


        TestInterface t = () -> System.out.println("t       这是一个无参的,无返回值的方法");
        t.testMethod();

        
        打印:
        ttt
        tt
        t       这是一个无参的,无返回值的方法
        
        /*
        注意1:如果方法体中只有一句话的时候,{}可以省略不写
         */

    }
}

 

语法结构02:有一个参数,无返回值:

package ..lambda.test02;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : -
 * 语法结构02:有一个参数,无返回值:
 */
public interface TestInterface02 {
    void testMethod(int num);
}

class Demo01{
    //这是main方法,程序的入口
    public static void main(String[] args) {

        TestInterface02 ttt = new TestInterface02() {
            @Override
            public void testMethod(int x) {
                System.out.println("这个方法的参数为:" + x);
            }
        };
        ttt.testMethod(30);


        TestInterface02 tt = (x) -> System.out.println("这个方法的参数为:" + x);
        tt.testMethod(20);


        TestInterface02 t = x -> System.out.println("这个方法的参数为:" + x);
        t.testMethod(10);
        
        

        打印:这个方法的参数为:30
              这个方法的参数为:20
              这个方法的参数为:10

        /*
        注意:
        1.参数名字随便起
        2.参数的类型可以省略 - 》上下文有类型推断
        3.参数只有一个的话,()可以省略
         */

    }
}

 

语法结构3:两个或者两个以上的参数,有返回值:

package ..lambda.test02;

/**
 * @author : -
 * 语法结构3:两个或者两个以上的参数,有返回值
 */
@FunctionalInterface
public interface TestInterface03 {
    int testMethod(int num1, int num2);
}

class Demo03{

    public static void main(String[] args) {

        TestInterface03 ttt = new TestInterface03() {
            @Override
            public int testMethod(int x, int y) {
                System.out.println("参数为: "+x+"   "+y);
                return 444;
            }
        };
        System.out.println(ttt.testMethod(10,20));


        TestInterface03 tt = (x, y) -> {
            System.out.println("两个或者两个以上的参数,有返回值" + x + "---" + y);
            return 555;
        };
        System.out.println(tt.testMethod(10,20));


        TestInterface03 t = (x,y) -> 666;
        System.out.println(t.testMethod(10, 20));
        
        
        打印:  参数为: 10   20
                444
                两个或者两个以上的参数,有返回值10---20
                555
                666

        /*
        1.多个参数的话,()不可以省略
        2.方法体中有多个逻辑的话,{}也不可以省略
        3.如果方法体中只有一句话,并且这句话是返回值那句的话,{}可以省略,return关键字也可以省略的

        总结:能省则省

        Lambda表达式使用的那个接口 ,有个特点:里面只有一个抽象方法 ----》函数式接口
        通过注解:@FunctionalInterface  来限定函数式接口

         */
    }
}

 

posted @ 2020-11-10 17:41  Li&Fan  阅读(117)  评论(0编辑  收藏  举报