函数接口

定义:接口中只有唯一的一个抽象方法,该接口就称之为函数接口。

//函数接口
public interface FunctionInterface1 {//1、只有一个方法的接口,默认称之为函数接口void get();
}//非函数接口
public interface FunctionInterface2 {void get1();void get2();
}

@FunctionInterface:
JDK 8推出了一个重要的注解@FunctionInterface
@FunctionInterface:作用主要用来强制约定一个接口只允许一个抽象方法。

@FunctionalInterface
public interface FunctionInterface {//1、只有一个方法的接口,默认称之为函数接口void get();//2、使用@FunctionInterface注解后,该接口只能有一个抽象方法,//   get2() 方法放开会报错//void get2();
}

函数接口中支持 default 和 static 关键字修饰我们的方法,允许存在Object类中equals方法。

@FunctionalInterface
public interface FunctionInterface {//1、只有一个方法的接口,默认称之为函数接口void get();//2、使用@FunctionInterface注解后,该接口只能有一个抽象方法,//void get2();//3、函数接口中支持 default 和 static 关键字修饰我们的方法default void defaultFunction(){System.out.println("我是default修饰的方法");}static void staticFunction(){System.out.println("我是static修饰的方法");}//4、重点:允许存在Object类中equals方法。public boolean equals(Object obj);}

JDK中存在的函数接口:
    Runnable:用于创建线程
    Comparator:用于比较对象