package unittest;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
@FunctionalInterface
interface IFuction1
{
public abstract P uppCase();
}
@FunctionalInterface
interface IFuction2
{
public abstract P uppCase(P p);
}
@FunctionalInterface
interface IFuction3{
public abstract R trans(P p);
}
public class TestLocal{
public static void main(String[] args) {
//利用方法引用的概念可以位一个方法定义多个名字,但是要求必须是函数式接口.
IFuction1<String> ifunction1 = "avc" :: toUpperCase;// public String toUpperCase() {
String str1 = ifunction1.uppCase();//实例对象::普通方法(不带参数)
System.out.println(str1);
IFuction2<String> ifunction2 = String :: toUpperCase; // public String toUpperCase() {
String str2 = ifunction2.uppCase("avc");//类名称::普通方法(带参数)
System.out.println(str2);
IFuction3<Integer,String> ifunction3 = String :: valueOf;//类名称::static方法(带参数) // public static String valueOf(int i) {
String str3 = ifunction3.trans(123456);
System.out.println(str3.length());
//内建函数式接口
//1.功能型内建函数式接口:有参数 有返回值
Function<String,Boolean> fun = "abc123"::startsWith;
System.out.println("1功能型内建函数式接口:" + fun.apply("abc"));
//2.消费型内建函数式接口:有参数 无返回值
Consumer<String> con = System.out::println;
con.accept("2消费型内建函数式接口:avc");
//3.供给型内建函数式接口:无参数 有返回值
Supplier<String> supplier = "abc"::toUpperCase;
System.out.println("3供给型内建函数式接口:" + supplier.get());
//4.断言型内建函数式接口:无参数 有返回值
Predicate<String> predicate = String::isEmpty;
System.out.println("4断言型内建函数式接口:" + predicate.test("a"));
Predicate<String> fun_predicate = "abc123"::startsWith;
System.out.println("5功能_断言型内建函数式接口:" + fun_predicate.test("abc"));
}
}