一、Interface接口增强

1.1 默认方法

1.概念:使用default关键字来修饰的方法
2.语法: 访问修饰符 default 返回值类型 方法名称(参数列表) { 方法体; return 返回值 }
3.说明:
A.接口中的默认方法必须通过实例化实例化来进行调用
B.接口的实现类不是强制重写接口中的默认方法
C.接口实现类中方法可以调用接口中的默认方法 调用语法:接口名称.super.方法名()

@FunctionalInterface
public interface XXX {
	//默认方法
    default int testDefaultMethod(int a,int b){
    	return a+b;
	}
}

1.2 静态方法

1.概念:使用static 进行修饰的方法就是静态方法
2.语法: 访问修饰符 static 返回值类型 方法名称(参数列表) { 方法体 return 返回值 }
3.说明
A.接口中静态方法直接可以通过接口的名称调用
B.实现类不能重写接口中静态方法

@FunctionalInterface
public interface XXX {
    //静态方法
    static int testStaticMethod(int a,int b){
    	return a+b;
	}
}

1.3 私有方法(JDK9)

1.概念:使用private来进行修饰的方法
2.语法:private 返回值类型 方法名称(参数列表) { 方法体 return 返回值 }
3.说明:
A.在jdk1.9之后才能使用私有的方法
B.实现类不能重写接口中私有的方法
C.接口中普通私有方法可以通过接口中默认的方法来间接访问
D.接口中私有的静态方法可以通过接口中静态方法来间接访问

二、Lambda表达式

2.1 简介

1.本质:就是匿名的接口
2.前提条件:必须是函数型接口 接口中只能有一个抽象方法 可以有其它的方法
3.作用:简化匿名内部类的写法
4.语法: (参数) -> {方法体 }
5.解释: (参数)> 表示的是接口中抽象方法中的参数 -> 没有特殊含义 指向方法体 方法体>接口方法执行的操作
6.简化方式
A.如果表达式只有一个参数 就可以省略数据类型以及括号
B.如果表达式中方法体只有一句话 可以省略大括号以及分号
C.如果表达式中方法体只有一句话是return 就可以省略return以及大括号以及分号

2.2 语法和简化

package com.xxx;

/**
 * Lambda表达式
 * @author 王则钰 Wyzel
 * @date 2022/10/25 15:59
 */
public class Program {
    public static void main(String[] args) {
        If1 if1 = () -> {
            System.out.println("无参数无返回值");
        };
        if1.test();

        If2 if2 = (int a) -> {
            System.out.println("一个参数无返回值");
        };
        if2.test(2);

        If3 if3 = (int a, int b) -> {
            System.out.println("两个参数无返回值");
        };
        if3.test(3, 3);

        If4 if4 = () -> {
            return 4;
        };
        System.out.println("无参数有返回值" + if4.test());

        If5 if5 = (int a) -> {
            return a;
        };
        System.out.println("一个参数有返回值" + if5.test(5));


        If6 if6 = (int a,int b) -> {
            return a + b;
        };
        //可通过Lambda简化成
//        If6 if6 = Integer::sum;
        System.out.println("多个参数有返回值" + if6.test(6, 6));

    }


    /**
     * 无参数无返回值
     */
    interface If1 {
        void test();
    }

    /**
     * 一个参数无返回值
     */
    interface If2 {
        void test(int a);
    }

    /**
     * 两个参数无返回值
     */
    interface If3 {
        void test(int a, int b);
    }

    /**
     * 无参数有返回值
     */
    interface If4 {
        int test();
    }

    /**
     * 一个参数有返回值
     */
    interface If5 {
        int test(int a);
    }

    /**
     * 多个参数有返回值
     */
    interface If6 {
        int test(int a, int b);
    }
}

package com.xxx;

/**
 * Lambda表达式简化
 * 1.参数类型可以省略
 * 2.只有1个参数时,可以省略()小括号
 * 3.如果方法体只有一条语句,{}花括号可以省略
 * 4.如果方法体中唯一的语句时return,那省略{}的同时,return也要省略
 * @author 王则钰 Wyzel
 * @date 2022/10/25 16:19
 * @Description
 */
public class Program2 {
    public static void main(String[] args) {
        /*
        没参数不能省略(),2个以上也不能省略()
        如果只有一个语句,{}可以省略
         */
        Program.If1 if1 = () ->
            System.out.println("无参数无返回值");
        if1.test();

        /*
        单个参数类型可以省略
        有1个参数可以省略()
        如果只有一个语句,{}可以省略
         */
        Program.If2 if2 = a ->
            System.out.println("一个参数无返回值");

        if2.test(2);

        /*
        多个参数类型也能省略,一起省略
        如果只有一个语句,{}可以省略
         */
        Program.If3 if3 = (a, b) ->
            System.out.println("两个参数无返回值");
        if3.test(3, 3);

        /*
        只有一条语句,{}和return一起省略
         */
        Program.If4 if4 = () -> 4;

        System.out.println("无参数有返回值" + if4.test());

        /*
        省略参数类型
        有1个参数可以省略()
        只有一条语句,{}和return一起省略
         */
        Program.If5 if5 = a -> a;

        System.out.println("一个参数有返回值" + if5.test(5));

        /*

         */
        Program.If6 if6 = Integer::sum;
        System.out.println("多个参数有返回值" + if6.test(6, 6));

    }


    /**
     * 无参数无返回值
     */
    interface If1 {
        void test();
    }

    /**
     * 一个参数无返回值
     */
    interface If2 {
        void test(int a);
    }

    /**
     * 两个参数无返回值
     */
    interface If3 {
        void test(int a, int b);
    }

    /**
     * 无参数有返回值
     */
    interface If4 {
        int test();
    }

    /**
     * 一个参数有返回值
     */
    interface If5 {
        int test(int a);
    }

    /**
     * 多个参数有返回值
     */
    interface If6 {
        int test(int a, int b);
    }

}

三、函数式接口

3.1 简介

  • 首先的是一个接口
  • 接口内有且只有一个抽象方法
  • 为防止破坏函数式接口,最好是在接口上使用
  • @FunctionalInterface注解修饰

定义一个函数接口

package com.xxx;

@FunctionalInterface
public interface Inner3 {
    //只能有一个抽象方法
    void showInfo();

    default void show() {
    }
}

3.1.1 函数接口作为方法的参数
3.1.2 函数接口作为方法的返回值
3.1.3 jdk提供的函数型接口作为方法的参数

3.2 供给型接口-Supplier

方法名称 方法描述
T get() 得到一个结果
package com.xxx;

import java.util.function.Supplier;

public class SupplierTest {
    public static void main(String[] args) {
        String str = getStr(() -> {
            return "函数式接口Supplier".substring(0, 1);
        });

        System.out.println(str);
    }

    public static String getStr(Supplier<String> supplier){
        return supplier.get();
    }
}

3.3 消费型接口-Consumer

方法名称 方法描述
void accept(T t) 提供一个消费的方法
default Consumer andThen(Consumer<? super T> after) 连接多个消费型接口
package com.xxx.lambda.function;

import java.util.function.Consumer;

public class ConsumerTest {
    public static void main(String[] args) {
        showInfo("函数式接口-消费型-Consumer",(String con) -> {
            System.out.println(con.substring(0, 1));
        });
    }

    public static void showInfo(String s, Consumer<String> con) {
        con.accept(s);
    }
}

package com.xxx.lambda.function;

import java.util.function.Consumer;

public class ConsumerTest2 {
    public static void main(String[] args) {
        getStr("asdfgQWERTY",(String con1) -> {
            
        },(String con2) -> {

        });
    }

    public static void getStr(String s, Consumer<String> consumer1, Consumer<String> consumer2){
        //andThen连接多个消费型接口
        //accept提供一个消费的方法
        consumer1.andThen(consumer2).accept(s);
    }
}

原文地址:http://www.cnblogs.com/wyzel/p/16826288.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性