Java8自定义函数式编程接口和便捷的引用类的构造器及方法

news/2024/7/7 12:54:08

什么是函数编程接口?

约束:抽象方法有且只有一个,即不能有多个抽象方法,在接口中覆写Object类中的public方法(如equal),不算是函数式接口的方法。

被@FunctionalInterface注解该接口,没有该注解的接口满足约束也行。

 

自定义一个函数式编程接口

/**
 * 自定义一个函数式编程接口
 * 函数式编程只有一个抽象方法,所以默认的是实现的是这个抽象方法
 * @param <T>
 * @param <R>
 */
@FunctionalInterface
public interface CalcFunctionInterface<T, R> {
    /**
     * 计算t1和t2
     *
     * @param t1
     * @param t2
     * @return
     */
    R calc(T t1, T t2);
}

 

传入不同calc函数实现的对象,进行调用

相当于以前创建CalcFunctionInterface的匿名类重写了calc方法(由于只有一个抽象方法,所以默认就是calc方法)

 /**
     * 相当于一个类实现了CalcFunction接口中的唯一一个函数calc
     * 然后在利用多态,调用calc函数,传入两个参数,进行计算
     */
    @Test
    public  void add(){
        CalcFunctionInterface<Integer, Integer> add = (t1, t2) -> t1+t2;
        Integer calc = add.calc(2, 3);
        System.out.println(calc);
        // 5
    }

 

传入一个匿名类对象,进行方法调用calc

@Test
    public void multiply(){
        // 相当于通过匿名类的形式传入一个实现了CalcFunctionInterface接口的子类对象,重写了该接口的方法
        Integer calc = FunctionalInterfacesTest.calc(2, 3, (t1, t2) -> t1 * t2);
        System.out.println(calc);
        // 6
    }

    /**
     * 接受了一个对象,利用对象的calc方法计算
     */
    public static Integer calc(Integer i1, Integer i2, CalcFunctionInterface<Integer, Integer> calc){
        return calc.calc(i1,i2);
    }

 

便捷的引用类的构造器及方法

一个Convert接口

@FunctionalInterface
public interface Convert<F, T> {
    T convert(F from);
}

 

lambda表达式的形式重写该函数式编程的唯一接口

@Test
    public void testLambda(){
        Convert<String, Integer> stringIntegerConvert = (from -> Integer.valueOf(from));
        Integer convert = stringIntegerConvert.convert("123");
        System.out.println(convert);
        // 123
    }

 

下面使用"::"运算符更精简

 

静态方法

@Test
    public void testStaticMethod(){
        Convert<String, Instant> stringInstantConvert = Instant::parse;
        Instant convert = stringInstantConvert.convert("2019-04-25T16:09:03.852Z");
        System.out.println(convert);
        // 2019-04-25T16:09:03.852Z
    }

 

实例方法

/**
     * 实例对象的方法
     */
    @Test
    public void testObjectMethod(){
        Something something = new Something();
        Convert<String, String> startsWith = something::startsWith;
        String convert = startsWith.convert("123");
        System.out.println(convert);
        // 1
    }
class Something {
    public String startsWith(String s) {
        return String.valueOf(s.charAt(0));
    }
}

 

对象的构造方法

 /**
     * 调用对象的构造方法
     */
    @Test
    public void testConstructor(){
        PersonFactory<Person> personFactory = Person::new;
        Person person = personFactory.create("Chris", "Paul");
        System.out.println(person);
        // Person(firstName=Chris, lastName=Paul)
    }
/**
 * Person 工厂
 */
@FunctionalInterface
public interface PersonFactory<P extends Person> {
    P create(String firstName, String lastName);
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
class Person {
    private String firstName;
    private String lastName;
}

 

Java8的内置函数式编程接口

https://www.cnblogs.com/theRhyme/p/10774341.html

https://mp.weixin.qq.com/s?__biz=MzIzMzgxOTQ5NA==&mid=2247483845&idx=1&sn=08990fd78e4f62ddf38238660cc4dd64&chksm=e8fe9dccdf8914da580e13a9fc5fee64c135f55d11f3c2de5731f052fa9f1a39060ed6375110&scene=21#wechat_redirect

https://www.exception.site/java8/java8-new-features

转载于:https://www.cnblogs.com/theRhyme/p/10697843.html


http://www.niftyadmin.cn/n/4556146.html

相关文章

02linux 基本命令

系统环境变量的设置 以nginx为例&#xff1a; 方式一&#xff1a; ~/.bash_profile 这是个人的 /etc/profile 为系统的环境变量 vim /etc/profile.d/nginx.sh#在该文件中配置&#xff1a; export PATH /opt/nginx-1-12/sbin:$PATH# 然后执行 . /etc/profile.d/nginx.sh# 然后…

解决 HomeBrew 下载缓慢的问题

macOS 自身不提供包管理器&#xff0c;常用的包管理器有 HomeBrewMacPortsMacPorts 第一次使用要 build 整个基本库&#xff0c;编译时间很长。优点是不怎么依赖系统&#xff0c;更新 macOS 不会破坏现有的包&#xff0c;以及可以使用所有的开源包。 HomeBrew 尽量使用系统现有…

谁是面向对象设计中的霸主?(上)

版权声明&#xff1a;本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u010929604/article/details/26051679 在面向对象的世界里&#xff0c;设计模式从来就没有最好最强的&#xff0c;各个模式从来就谁也不服谁。彼此明争暗斗&#xff0c;未曾善罢甘休。今…

Linux虚拟系统安装——Ubuntu18.04 CentOS6.5

Linux虚拟系统安装——Ubuntu18.04 & CentOS6.5 摘要&#xff1a;Linux简介、虚拟系统安装、系统备份与文件介绍 1. Linux简介 &#xff08;1&#xff09;1968年&#xff0c;MIT、Bell实验室、美国通用电气公司共同开发Multic项目&#xff1b; &#xff08;2&#xff09;19…

贪吃蛇(C语言)

Snake.c #include<stdio.h> #include<stdlib.h> #include <time.h> #include <windows.h> #include <conio.h>

VUE 动态修改Video标签Src播放路径

首先HTML&#xff0c;注意这里修改 bind绑定src 属性是不能生效的 我使用的是用JS 获取DOM节点&#xff0c;然后给DOM的Src属性赋值 <video id"playVideos" controls width"100%" height"240"><source id"playVideosss" :src…

六.类的封装

类的封装 源程序&#xff1a; package lianxi;public class Account {String zhanghao;double cunkuanyu_e;Account (String hao,double a){zhanghaohao;cunkuanyu_ea; }//存款之后 并得到余额 //有返回值带参数的方法double cunkuan(Double cunqian) { if(cunqian<0)…

独立看第一个C++程序到最终结果log----2019-04-15

本文纯为本人记录&#xff0c;有网上诸多参考&#xff0c;请勿转发&#xff01; 记录可能可能有点啰嗦&#xff0c;自己划重点吧&#xff01;&#xff01; &#xff08;无论是生活还是工作&#xff0c;如果很困惑&#xff0c;千万不要消极一定要勇敢积极的面对它&#xff0c;不…