zoukankan      html  css  js  c++  java
  • JDK系列2:函数式编程案例

    一、简介:

    函数式编程是种编程方式,它将电脑运算视为函数的计算。函数编程语言最重要的基础是λ演算(lambda calculus),而且λ演算的函数可以接受函数当作输入(参数)和输出(返回值)。其中,函数式接口是只包含一个方法的接口。

     

    二、案例:

    编写无参的函数式接口:

    1、自定义一个函数式接口(无参的)

    @FunctionalInterface
    public interface CustomInterface {
        void print();
    }

    2、编写业务类

    public class CustomService{
        
        @Test
        public void test(){
            // 编写业务逻辑,并将业务逻辑作为参数传递给print方法
            print(()->{        //由于CustomInterface的print()接口是无参的所以写个括号就行
                System.out.println("----------");
            });
        }
        
        private void print(CustomInterface c){
            c.print();    // 这里可以这样理解,把接收到的业务逻辑嵌套进print()方法中
        }
    }

    编写有参的函数式接口:

    1、自定义一个函数式接口(有参的)

    @FunctionalInterface
    public interface CustomInterface {
        void print(List<String> list);
    }

    2、编写业务类

    public class CustomService{
        @Test
        public void test() { 
            // 编写业务逻辑:遍历list并输出。将业务逻辑作为参数传递给print方法
            // 由于CustomInterface的print()接口是有参的,所以要对应起来
            print(list -> list.forEach(System.out::println));
        }
        
        private void study(CustomInterface c) {
            List<String> list = Arrays.asList("a","b","c");
            c.print(list);    // 这里可以这样理解,把接收到的业务逻辑嵌套进print()方法中
        }
      }
  • 相关阅读:
    前端开发中同步和异步的区别
    SQL STUFF函数 拼接字符串
    Javascript的精华
    .net网站报错:对象的当前状态使该操作无效
    免费 WebOffice使用
    DotNetBar教程
    C#获取周的第一天、最后一天、月第一天和最后一天
    C#判断文字是否为汉字
    C# 将字符串转为&#2345;这种的 html实体编码
    SqlServer将没有log文件的数据库文件附加到服务器中
  • 原文地址:https://www.cnblogs.com/XueTing/p/13769487.html
Copyright © 2011-2022 走看看