标签:
1.Lamdba表达式与匿名内部类:Lamdba表达式的主要作用是代替匿名内部类的繁琐语法,由三部分构成:(形式参数)->(代码块),其中形参和代码块的括号有时可以省。
例:
1 package Lambda; 2 3 public interface Run { 4 public String run(String weather); 5 6 }
1 package Lambda; 2 3 public interface Eat { 4 public void eat(); 5 6 }
1 package Lambda; 2 3 public interface Stand {
1 package Lambda; 2 3 public class Lambda_os { 4 public void eat_1(Eat e){ 5 e.eat(); 6 } 7 public void run_2(Run r){ 8 String str="uuuu"; 9 r.run(str); 10 } 11 public void stand_2(Stand s){ 12 System.out.println(s.add(2, 4)); 13 } 14 15 public static void main(String args[]){ 16 Lambda_os l=new Lambda_os(); 17 // l.eat_1(new Eat(){ 18 // public void eat(){ 19 // System.out.println("sssssssss"); 20 // } 21 // }); 22 l.eat_1(()->{ 23 System.out.println("苹果味道不错"); 24 }); 25 l.run_2(weather->{ 26 System.out.println("今天的天气是"+weather); 27 return weather; 28 }); 29 l.stand_2((a,b)->{ 30 return (a+b); 31 }); 32 } 33 34 }
4 public int add(int a,int b); 5 6 7 }
2.Lamdba表达式也被称为”目标类型“,"目标类型"必须是”函数式接口“,”函数式接口“的意思是只包含一个抽象方法的接口
标签:
原文地址:http://www.cnblogs.com/abstract-fabulous/p/5430336.html