package lambda;publicclassTestLambda02{publicstaticvoidmain(String[] args){
ILove love =newLove();
love.love(9);}}interfaceILove{voidlove(int a);}//1.实现类classLoveimplementsILove{@Overridepublicvoidlove(int a){
System.out.println("I love you -->"+a);}}
b.定义函数式接口,实现静态内部类来创建该接口的对象
package lambda;publicclassTestLambda02{//静态内部类staticclassLoveimplementsILove{@Overridepublicvoidlove(int a){
System.out.println("I love you -->"+a);}}publicstaticvoidmain(String[] args){
ILove love =newLove();
love.love(8);}}interfaceILove{voidlove(int a);}
c.定义函数式接口,实现局部内部类来创建该接口的对象
package lambda;publicclassTestLambda02{publicstaticvoidmain(String[] args){//局部内部类classLoveimplementsILove{@Overridepublicvoidlove(int a){
System.out.println("I love you -->"+a);}}
ILove love =newLove();
love.love(6);}}interfaceILove{voidlove(int a);}
d.定义函数式接口,匿名内部类创建该接口的对象
package lambda;publicclassTestLambda02{publicstaticvoidmain(String[] args){//匿名内部类,没有类的名称,必须借助接口或者父类
ILove love =newILove(){@Overridepublicvoidlove(int a){
System.out.println("I love you -->"+a);}};
love.love(3);}}interfaceILove{voidlove(int a);}
e.定义函数式接口,lambda创建该接口的对象
package lambda;publicclassTestLambda02{publicstaticvoidmain(String[] args){//用lambda简化
ILove love =(int a)->{ System.out.println("I love you -->"+a);};
love.love(1);}}interfaceILove{voidlove(int a);}
4.Lambda表达式简化
package lambda;publicclassTestLambda02{publicstaticvoidmain(String[] args){//用lambda简化
ILove love =(int a)->{ System.out.println("I love you -->"+a);};
love.love(1);//去掉参数类型简化
love =(a)->{ System.out.println("I love you -->"+a);};
love.love(2);//去掉括号简化,只有单个参数的时候可以不用括号
love = a->{ System.out.println("I love you -->"+a);};
love.love(3);//去掉花括号简化,只有单行代码的时候可以不用花括号
love =(a)->System.out.println("I love you -->"+a);
love.love(4);}}interfaceILove{voidlove(int a);}