引用数据类型最大的特点是可以进行内存的指向处理 不同方法名描述同一个方法
自有定义:
@FunctionalInterface
public interface Function<T,R>{
public R apply(T t) ;
}
@FunctionalInterface
public interface Consumer<T>{
public void accept(T t) ;
}
@FunctionalInterface
public interface Supplier<T>{
public T get() ;
}
//断言函数式接口 进行判断处理 equalsIgnareCase()
@FunctionalInterface
public interface Predicate<T>{
public boolean test(T t) ;
}
代码:
package com.msc.example;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
interface IFunction<K,R>{
public K change(R r);
}
interface IFunctionb<R>{
public R upper();
}
interface IFunctionc<P>{
public int compare(P p1,P p2);
}
interface IFunctiond<P>{
public P create(String n,int a);
}
class Example{
private String name ;
private int age ;
public Example(String namr,int age){
this.age = age ;
this.name = name ;
}
@Override
public String toString() {
return "Example{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class FunctionDemo {
public static void main(String[] args) {
IFunction<String,Integer> fun = String :: valueOf;
IFunctionb<String> funb = "test" :: toUpperCase;
IFunctionc<String> func = String :: compareTo;
IFunctiond<Example> fund = Example::new ;
String str = fun.change(100) ;
System.out.println(str);
System.out.println(funb.upper());
System.out.println(func.compare("A","a"));
System.out.println(fund.create("里斯",20));
System.out.println("-------------------------------");
Function<String,Boolean> funl = "**hello" :: startsWith;
System.out.println(funl.apply("**"));
Consumer<String> con = System.out :: println;
con.accept("Consumer");
Supplier<String> sup = "com.mSc.cn" :: toLowerCase;
System.out.println(sup.get());
Predicate<String> pre = "com" :: equalsIgnoreCase;
System.out.println(pre.test("Com"));
}
}