Java8 新特性02-方法引入

方法引入

什么是方法

方法引用:需要结合lambda表达式能够让代码变得更加精简。

  1. 匿名内部类使用
  2. Lambda调用匿名内部类
  3. 方法引入
方法引用
  1. 静态方法引入: 类名::(静态)方法名称
  2. 对象方法引入 类名:: 实例方法名称
  3. 实例方法引入 new对象 对象实例::方法引入
  4. 构造函数引入 类名::new

需要遵循一个规范:

方法引入 方法参数列表、返回类型与函数接口参数列表与返回类型必须要保持一致

类型语法对应lambda表达式
构造器引用Class::new(args) -> new 类名(args)
静态方法引用Class::static_method(args) -> 类名.static_method(args)
对象方法引用Class::method(inst,args) -> 类名.method(args)
实例方法引用instance::method(args) -> instance.method(args)

方法引用提供了非常有用的语法, 可以直接引用已有的Java类或对象的方法或构造器, 方法引用其实也离不开lambda表达式

当lambda联合使用.减少代码的冗余

方法引用提供非常有用的语法 ,可以直接引用已有的Java类或者对象中方法或者构造函数方法引用需要配合lambda表达式语法一起使用减少代码的冗余性问题

  1. 构造器引用
  2. 静态引用
  3. 对象方法引用
  4. 实例方法引用
MayiktService
public interface MayiktService {
    String get(Test23 test23);
}


MessageInterface
@FunctionalInterface
public interface MessageInterface {
  void   get(Integer var1, Integer var2);
}


MessageInterface2
@FunctionalInterface
public interface MessageInterface2 {
    String getMessage();
}


MessageInterface3
@FunctionalInterface
public interface MessageInterface3 {
   MessageEntity getMessage();
}
静态方法引入
public class TestLL01 {
    public static void main(String[] args) {
        //最原生的调用
//        new MessageInterface() {
//            @Override
//            public String get(Integer var1, Integer var2) {
//                System.out.println(ssss);;
//            }
//        }
        //方法引入
        MessageInterface staticGet = TestLL01::staticGet;
        staticGet.get(12, 20);
    }
    public static void staticGet(Integer a, Integer b) {
        System.out.println("staticGet,a:" + a + "+++" + b);
    }
}

实例对象引用
public class TestLL02 {
    /**
     * 实例对象引用
     *
     * @param args
     */
    public static void main(String[] args) {
//        MessageInterface2 messageInterface2 = () -> {
//            return "dasdasd";
//        };
//        System.out.println(messageInterface2.getMessage());
        TestLL02 testLL02 = new TestLL02();
        String object = testLL02.object();
        System.out.println(object);
    }

    public String object() {
        return "dadasda";
    }
}

构造函数引入
public class TestLL03 {
    /**
     * 构造函数引用
     *
     * @param args
     */
    public static void main(String[] args) {
        MessageInterface3 messageInterface3 = () -> new MessageEntity();
        AcanthopanaxInterface aNew = MessageEntity::new;
        System.out.println(aNew);
    }
}
对象方法引入
public class Test23 {
    public static void main(String[] args) {
        //1. 使用匿名内部类的形式
        MayiktService mayiktService = new MayiktService() {
            @Override
            public String get(Test23 test23) {
                return test23.object();
            }
        };
        System.out.println(mayiktService.get(new Test23()));
        //2.lambda
        MayiktService mayiktService2 = test23 -> test23.object();
        System.out.println(mayiktService2.get(new Test23()));

        //3.方法引用 这哥时候函数接口 第一个参数传递test23 返回调用test23.object方法
        MayiktService mayiktService3 = Test23::object;
        System.out.println(mayiktService3.get(new Test23()));

        // R apply(T t); T applu方法传递的参数类型 : R apply 方法返回的类型
        //需要降string 类型字符串获取长度
//        Function<String, Integer> stringIntegerFunction = (str) -> {
//            return str.length();
//        };
        Function<String, Integer> stringIntegerFunction1 = String::length;
        System.out.println(stringIntegerFunction1.apply("dasdadasd"));
    }

    private String object() {
        return "niwwewe";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值