首页 > 试题广场 >

定义一个函数式接口,并使用 Lambda表达式创建函数式接口

[问答题]
定义一个函数式接口,并使用 Lambda表达式创建函数式接口的实例。
    interface Math{
        Double add(Double a, Double b);
    }


    public static void Print1(Double a, Double b ,Math math){
        System.out.println(a + " + " + b + " = " + math.add(a, b));
    }


    public static void main(String[] args) {
        Print1(1d,2d, (x, y) -> x + y);
    }

编辑于 2019-11-21 11:29:51 回复(0)
interface Aoo{
    void show();
}
class Boo implements Aoo{

    @Override
    public void show() {
        System.out.println("lambda");    
    }
    public static void main(String[] args) {
        Aoo a = ()->System.out.println("lambda");
    }
    
}

lambda创建匿名内部类时实现的接口必须只能有一个抽象方法,否则不可以使用
Aoo a = ()->System.out.println("lambda");
等于:
Aoo a = new Aoo(){
            public void show() {
                System.out.println("lambda");    
            }
        };

发表于 2019-10-16 19:53:00 回复(1)
你这是类,不是接口啊
发表于 2019-10-10 20:00:53 回复(0)
public class Mathes{
    public Mathes(Object obj) {
        Object obj1 = new Object();
        Object obj2 = (Runnable)() ->{
            for(int i = 0;i <100;i++) {
                System.out.println();
            }
        };
    }
}

发表于 2019-09-27 22:04:23 回复(0)