java常用函数接口
package com.bilibili;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* @author WangYH
* @version 2021.1.3
* @date 2023/4/7 15:19
*/
public class Main {
public static void main(String[] args) {
//规定一些框架
//供给型函数接口,提供对象
Supplier<Student> studentSupplier = Student::new;
//特别简洁,lambda表达式可以替换成方法引用
studentSupplier.get().show();
//消费型函数接口,使用对象
Student student = new Student();
STUDENT_CONSUMER.andThen(st -> System.out.println("后续操作")).accept(student);
//Function,消费一个对象,对外供给一个对象
//compose前置操作,andThen后置操作
student.score = 70;
boolean b = STUDENT_PREDICATE.and(stu -> stu.score > 90).test(student);
//需要同时满足and条件才会返回 true
//and or not操作
if (!b) {
System.out.println("Java还得多学");
}
//使用这些函数接口,可以使得代码更加简洁易读
}
public static class Student{
int score;
public void show() {
System.out.println("我是学生!!");
}
}
private static final Consumer<Student> STUDENT_CONSUMER = student -> System.out.println(student.toString() + " 好好吃饭!");
private static final Predicate<Student> STUDENT_PREDICATE = student -> student.score >= 60;
}