packagecom.shisan.lambda;//推到lambda表达式publicclassTestLambda1{//3.静态内部类staticclassLike1implementsILike{@Overridepublicvoidlambda(){System.out.println("i like lambda1");}}publicstaticvoidmain(String[] args){ILike like =newLike();
like.lambda();
like =newLike1();
like.lambda();//4. 局部内部类classLike2implementsILike{@Overridepublicvoidlambda(){System.out.println("i like lambda2");}}
like =newLike2();
like.lambda();//5. 匿名内部类 没有类的名称,必须借助接口或者父类
like =newILike(){@Overridepublicvoidlambda(){System.out.println("i like lambda3");}};
like.lambda();//6. 用lambda简化
like =()->{System.out.println("i like lambda4");};
like.lambda();}}//1.定义一个函数式接口interfaceILike{voidlambda();}//2.实现类classLikeimplementsILike{@Overridepublicvoidlambda(){System.out.println("i like lambda");}}
lambda表达式简化
packagecom.shisan.lambda;publicclassTestLambda2{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(521);}}interfaceILove{voidlove(int a);}