Lamada表达式小技巧介绍
函数式编程
@FunctionalInterface interface Lf{ void dispaly(); }
@FunctionalInterface为显示定义函数时编程接口,不符合函数式编程接口会报错匿名内部类使用方式
public class FunPrograming { public static void main(String[] args) { func func = new func() { @Override public void dispaly() { // TODO Auto-generated method stub System.out.println("dispaly()运行"); } }; func.dispaly(); } } dispaly()运行
lamada: 拷贝小括号,写死->,落地大括号{}
public class FunPrograming { public static void main(String[] args) { func func = ()->{ System.out.println("dispaly()运行"); }; func.dispaly(); } } dispaly()运行
带返回值的接口
@FunctionalInterface interface func {//Invalid ‘@FunctionalInterface‘ annotation; func is not a functional interface定义两个普通方法就会报错,不符合函数编程 public void dispaly(); public int add(int x,int y); }
//拷贝小括号,写死->,落地大括号{}@FunctionalInterface interface func { //public void dispaly(); public int add(int x,int y); } public class FunPrograming { public static void main(String[] args) { func func = (int x,int y)->{ System.out.println("add(int x,int y)运行"); return x+y; }; System.out.println(func.add(3,5)); } } add(int x,int y)运行 8
函数式编程接口中如何定义多个方法
@FunctionalInterface interface func { default void dispaly() { System.out.println("default void dispaly()"); } default int mul(int x ,int y) { System.out.println("default int mul"); return x* y; } static int dec(int x,int y) { System.out.println("static int dec(int x,int y)"); return x-y; } static int div(int x,int y) { System.out.println("static int div(int x,int y)"); return x/y; } public int add(int x,int y); }
相关推荐
Jruing 2020-11-01
89231645 2020-10-26
87204154 2020-09-24
81244053 2020-09-23
FalseNotFalse 2020-09-22
81540398 2020-09-04
84423067 2020-06-12
hongbing 2020-06-02
huavhuahua 2020-05-11
samsai00 2020-05-06
猛禽的编程艺术 2020-04-23
上海滩 2020-04-22
斑点喵 2020-03-04
cuiguanjun 2020-03-01
TheBigBlue 2020-02-20
banzhihuanyu 2020-02-19
banzhihuanyu 2020-02-15
89510196 2020-02-06
banzhihuanyu 2020-01-31