一、函式式介面簡介
函式式介面(Functional Interface)就是一個有且僅有一個抽象方法,但是可以有多個非抽象方法的介面,
函式式介面可以被隱式轉換為 lambda 運算式,
Lambda 運算式和方法參考(實際上也可認為是Lambda運算式)上,
如定義了一個函式式介面如下:
package com.panasonic.mes.guor.service;
@FunctionalInterface
public interface CalFunctionalInterface<T, R> {
R cal(T t1, T t2);
}
二、Java8自帶的一些函式式介面
1、功能性介面Function
2、斷言性介面:Predicate
3、供給性介面:Supplier
4、消費性介面:Consumer
這四大介面的介紹,這里我就不多做解釋了,不想重復發文,
大家可以去我的【Java8 新特性 2】函式式介面 + Lamda運算式推導程序學習,
三、自定義函式式介面
1、簡單的計算型別
@FunctionalInterface
public interface CalFunctionalInterface<T, R> {
R cal(T t1, T t2);
}
/**
* 通過自定義函式式介面實作數值運算
*
* @return 3
*/
private static void calTest01() {
CalFunctionalInterface<Integer, Integer> add = (t1, t2) -> t1 + t2;
Integer cal = add.cal(1, 2);
System.out.println(cal.toString());
}
/**
* 將函式式介面當引數傳入 test02(2, 3, (t1, t2) -> t1 * t2);
*
* @param t1
* @param t2
* @param calFI
* @return 6
*/
private static void calTest02(Integer t1, Integer t2, CalFunctionalInterface<Integer, Integer> calFI) {
Integer cal = calFI.cal(t1, t2);
System.out.println(cal.toString());
}
2、型別轉換
package com.example.demo.java8;
@FunctionalInterface
public interface ConvertFunctionInterface<T, R> {
R convert(T t);
}
/**
* 通過自定義函式式介面實作資料型別轉換
*
* @return 24
*/
private static void convertTest01() {
ConvertFunctionInterface<Integer, String> convert = t -> String.valueOf(t);
String ret = convert.convert(2);
System.out.println(ret+4);
}
/**
* 使用"::"運算子更精簡
*
* @return 24
*/
private static void convertTest02() {
ConvertFunctionInterface<Integer, String> convert = String::valueOf;
String ret = convert.convert(2);
System.out.println(ret+4);//24
}
/**
* 使用"::"運算子更精簡
* convertTest03(2, String::valueOf);
*
* @return 24
*/
private static void convertTest03(Integer t, ConvertFunctionInterface<Integer, String> convertFI) {
String ret = convertFI.convert(t) + 4;
System.out.println(ret);
}
/**
* convertTest04("myFunctionInterfaceTest", ObjectUtil::hump2Line);
*
* @param t
* @param convertFI
* @return my_function_interface_test
*/
private static void convertTest04(String t, ConvertFunctionInterface<String, String> convertFI) {
String ret = convertFI.convert(t);
System.out.println(ret);
}
java8提供了Function介面(接收一個功能引數t,并回傳一個功能結果R,)
所以,一定要了解清楚,你要定義的方法,是否已經存在,不要重復造輪子,撰寫代碼的大忌,費力不討好,
/**
* java8提供了Function介面(接收一個功能引數t,并回傳一個功能結果R,)
*
* convertTest05("myFunctionInterfaceTest", ObjectUtil::hump2Line);
*
* @param t
* @param convertFI
* @return my_function_interface_test
*/
private static void functionTest05(String t, Function<String, String> function) {
String ret = function.apply(t);
System.out.println(ret);//my_function_interface_test
}
3、方法型介面
@FunctionalInterface
public interface LogFunction {
public void log();
}
public static void logTest01(LogFunction logFunction){
logFunction.log();
}
public static void logTest011(){
//呼叫log方法,方法的引數是一個介面,所以可以傳遞介面的實作類物件
logTest01(new LogFunction() {
@Override
public void log() {
System.out.println("函式式介面練習");
}
});
}
//呼叫log方法,方法的引數是一個函式式介面,所以,可以使用lambda運算式
public static void logTest012(){
//呼叫log方法,方法的引數是一個介面,所以可以傳遞介面的實作類物件
logTest01(() -> {
System.out.println("函式式介面練習");
});
}
//簡化lambda運算式
public static void logTest013(){
logTest01(() -> System.out.println("函式式介面練習"));
}
4、帶泛型的方法型函式
package com.example.demo.java8;
@FunctionalInterface
public interface LogFunction2<T extends Student> {
public void log(T t);
}
public static void logTest02(Student student, LogFunction2 logFunction2){
logFunction2.log(student);
}
public static void logTest021(Student student){
logTest02(student, new LogFunction2<Student>() {
@Override
public void log(Student t) {
String ret = t.getId() + "->" + t.getName() + "->" + t.getAge();
System.out.println(ret);//1->z->18
}
});
}
// lambda運算式寫法
public static void logTest022(Student student){
logTest02(student, (Student t) -> {
String ret = t.getId() + "->" + t.getName() + "->" + t.getAge();
System.out.println(ret);
});
}
5、帶多個泛型的功能型函式
package com.example.demo.java8;
@FunctionalInterface
public interface LogFunction3<T extends Student, R extends Teacher> {
public R build(T t);
}
public static void logTest03(Student student, LogFunction3 logFunction3){
logFunction3.build(student);
}
// Teacher(id=1, name=z)
public static void logTest031(Student student){
logTest03(student, new LogFunction3<Student, Teacher>() {
@Override
public Teacher build(Student s) {
Teacher t = new Teacher();
t.setId(s.getTeacherId());
t.setName(s.getTeacherName());
System.out.println(t);
return t;
}
});
}
6、物體類
package com.example.demo.java8;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer teacherId;
private String teacherName;
}
往期精彩內容:
Java知識體系總結
【全堆疊最全Java框架總結】SSH、SSM、Springboot
超詳細的springBoot學習筆記
常見資料結構與演算法整理總結
Java設計模式:23種設計模式全面決議
Java面試題總結(附答案)
10萬字208道Java經典面試題總結(附答案,建議收藏)
MySql知識體系總結
Linux知識體系總結
Redis知識體系總結
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/292329.html
標籤:java
上一篇:# Day08-Java基礎
下一篇:java八大基本資料型別與運算子
