java8接口

// 可以用来做工具类
// 这个注解是函数式注解,表示这个接口里面有且仅有一个抽象方法, 默认方法可以有0个或多个
@FunctionalInterface
public interface InterfaceDemo1<T, R> {
/**
* 等于 public static final int i = 0;
*/
int i = 0;

/**
* 此方法相当于 public int med1(String s) {...}
* @param s
* @return
*/
default int med1(String s) {
System.out.println(s);
return 10;
}

/**
* 此方法相当于 public abstract String med2(int s);
* @param t
* @return
*/
R med2(T t);

/**
* 此方法相当于 public static String med3(String s) {...}
* @param s
* @return
*/
static String med3(String s) {
return "aa1" + s;
}
}



为了免去用户每次使用Lambda表达式时,都自行创建函数式接口,Java提供了4大核心内置函数式接口

Consumer<T> :消费型接口

void accept(T t);

 

Supplier<T> :供给型接口

T get();

 

Function<T,R> :函数型接口

R apply(T t);

 

Predicate<T> :断言型接口

boolean test(T t)

posted @ 2019-10-15 15:29  剑阁丶神灯  阅读(241)  评论(0编辑  收藏  举报