java8-静态方法引用

来源:互联网 发布:淘宝企业店开店流程 编辑:程序博客网 时间:2024/05/21 22:28
/** * 静态方法引用 *  *  * 如果函数式接口的实现恰好可以通过调用一个静态方法完成 就可以使用静态方法引用 * (只是调用 不做其他处理) * ClassName: Exmaple1 <br/> * Function: TODO ADD FUNCTION. <br/> * date: 2017年9月8日 下午10:39:06 <br/> * @author: Lelonta * @version: *  *  * 类名::staticmethod */public class StaticRef {    //无参数的静态方法    public static String get() {            return "hello";        }    //有参数的静态方法    public static String put(String str) {        return str+"hello";    }    public static void main(String[] args) {        //普通lambda表达式   返回hello  静态方法get返回hello        Supplier<String> s1 = () -> "hello";        //改进        Supplier<String> s2 = () -> StaticRef.get();        //静态方法的引用        //我的方法要返回一个字符串   有一个静态方法刚好返回这个字符串        //此时 可以使用静态方法的引用        //只是引用 不做增删改的操作        Supplier<String> s3 = StaticRef::get;        System.out.println(s3.get());        //带有参数和返回值的表达式转换        Function<String, String> fn = (str) -> "hello";        Function<String, String> fn1 = StaticRef::put;        System.out.println(fn1.apply("world"));        BiFunction<String, String, Integer> bf = (String ss1,String ss2) -> ss1.length()+ss2.length();         System.out.println("bf"+"========="+bf.apply("abcdf", "nsnanda"));        BiFunction<String, String, Integer> bf1 =Bifuntion::get;        System.out.println("bf1"+"========="+bf1.apply("abcdf", "nsnanda"));    }}class Bifuntion {    public static int get(String string1,String string2) {        return string1.length() + string2.length();    }}