函数式接口Function(一)


Function 函数式接口初识


 

使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口。函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。

1.代码示例

@FunctionalInterface
public interface BranchHandle {


    void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);


}
public class VUtils {



    public static ThrowExceptionFunction isTure(boolean b){

        return (errorMessage) ->{

            if(b){
                throw new RuntimeException(errorMessage);
            }

        };

    }


    public static BranchHandle isTureOrFalse(boolean b){

        return (trueHandle, falseHandle) -> {
            if (b){
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }

}
  VUtils.isTureOrFalse(false).trueOrFalseHandle(() -> {
            System.out.println("true handler ....");
        }, () -> {
            System.out.println("false handler ....");
        });

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM