函數接口
定義:接口中只有唯一的一個抽象方法,該接口就稱之為函數接口。
//函數接口
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:用于比較對象