函数式接口与lambda表达式

函数式接口:只有一个方法的接口

public interface CanAdd {
    int add(int a,int b);
}

该接口作为其他方法的入参,实现函数传递

public class Dog {

    public static void add(int a,int b,CanAdd c){
        System.out.println(c.add(a, b));
    }

}

使用lambda传入函数

    public static void main(String[] args) {
        Dog.add(2,4,(int a,int b)->a+b);
    }
posted @ 2022-11-03 13:47  无极是一种信仰  阅读(5)  评论(0编辑  收藏  举报