1、为何要是用Lamda表达式
使代码更加简洁
避免定义过多的匿名内部类
去掉一堆作用不大的代码,只留下核心的逻辑
函数式接口的定义:
任何接口,只包含唯一的一个抽象方法,那它就是一个函数式接口
1 public class Lamda表达式 2 { 3 //静态内部类 4 static class Like2 implements Ilike{ 5 @Override 6 public void Like(){ 7 System.out.println("I Not Like Study"); 8 } 9 }10 public static void main(String[] args) {11 Ilike like =new Likestudy();12 like.Like();13 //局部内部类14 class Likestudy1 implements Ilike{15 @Override16 public void Like(){17 System.out.println("I Like Study 2");18 }19 }20 like =new Likestudy1();21 like.Like();22 //匿名内部类 无类名,必须借助接口或者父类23 like = new Ilike(){24 @Override25 public void Like(){26 System.out.println("I Like Study 3");27 }28 };29 like.Like();30 like = new Like2();31 like.Like();32 //Lamda表达式 简洁,但可读性差33 like=()->{34 System.out.println("I Creazy Like Study");35 };36 like.Like();37 38 }39 }40 //定义函数式接口41 interface Ilike {42 void Like();43 }44 class Likestudy implements Ilike{45 @Override46 public void Like(){47 System.out.println("I Like Study 1");48 };49 }