/*
推导Lambda表达式
*/publicclassTestLambda01{//3. 静态内部类staticclassLike2implementsILike{@Overridepublicvoidlambda(){
System.out.println("I like Lambda2.");}}publicstaticvoidmain(String[] args){
ILike ilike =newLike();
ilike.lambda();
ilike =newLike2();
ilike.lambda();//4. 局部内部类classLike3implementsILike{@Overridepublicvoidlambda(){
System.out.println("I like Lambda3.");}}
ilike =newLike3();
ilike.lambda();//5.匿名内部类:没有类的名称,必须借助接口或者父类
ilike =newILike(){@Overridepublicvoidlambda(){
System.out.println("I like Lambda4.");}};
ilike.lambda();//6.用Lambda简化
ilike =()->{
System.out.println("I like Lambda5.");};
ilike.lambda();}}//1.定义一个函数式接口interfaceILike{voidlambda();}//2.实现类classLikeimplementsILike{@Overridepublicvoidlambda(){
System.out.println("I like Lambda.");}}
Lambda简化
总结:
①lambda表达式只能有一行代码的情况下才能简化成一行;如果有多行,就要用代码块包裹
②前提是接口是函数式接口
③多个参数也可以去掉参数类型,要去掉都去掉
publicclassTestLambda02{//Lambda表达式简化publicstaticvoidmain(String[] args){
ILove love =(int a)->{
System.out.println("I love you ->"+ a);};//简化1.参数类型
love =(a)->{
System.out.println("I love you ->"+ a);};//简化2.括号
love = a ->{
System.out.println("I love you ->"+ a);};//简化3.花括号
love = a -> System.out.println("I love you ->"+ a);
love.love(1);}}interfaceILove{voidlove(int a);}