java Lambda表达式用法之函数式接口

2020-06-15 作者:血花飞舞

  Lambda表达式实现的接口不是普通的接口,称为是函数式接口,这种接口只能有一个方法。

  如果接口中声明多个抽象方法,就会报错误

  The target type of this expression must be a functional interface

示例代码如下:

  1. public class lambdaTest {
  2. @Test
  3. public void test() {
  4. //显示计算结果
  5. display((a,b)->{
  6. return a + b;
  7. },1,2);
  8. }
  9. /**
  10. *
  11. * @param c Lambda表达式
  12. * @param a
  13. * @param b
  14. */
  15. public static void display(Calc c,int a, int b) {
  16. System.out.println(c.calcInt(a, b));
  17. }
  18. /**
  19. * 函数式接口
  20. * @author busifox
  21. *
  22. */
  23. public interface Calc {
  24. int calcInt(int a, int b);
  25. }
  26. }