Lambda表达式实现


1
public interface FOO { 2 // public void sayhello(); 3 public int add(int x,int y); 4 5 default int mul(int x,int y){ 6 return x*y; 7 }; 8 9 public static int div(int x,int y) 10 { 11 return x/y; 12 } 13 14 public class LambdaDemo { 15 //接口只有一个抽象方法时使用,JAVA8之后接口允许一部分(default或static)方法实现 16 //Lambda:拷贝小括号(写参数或空),写死右箭头,落地大括号 ()->{} 17 18 public static void main(String[] args) { 19 // FOO foo = ()-> {System.out.println(x + y);return x+y;}; 20 FOO foo = (int x,int y)-> {System.out.println(x + y);return x+y;}; 21 22 foo.add(1,2); 23 }

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

函数式接口可以被隐式转换为 lambda 表达式。

猜你喜欢

转载自www.cnblogs.com/sbnj7/p/12442964.html