java8之方法引用

 XML Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.lyzx.day02;

import org.junit.Test;


public class T5{
    
    
     /**
     * 方法引用
     * 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
     * (实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!)
     * 方法引用:使用操作符  "::" 将方法名和对象或类的名字分隔开来。
     * 如下三种主要使用情况:
     * 对象::实例方法
     * 类::静态方法
     * 类::实例方法
     * /
    @Test
    public void test1(){
         //加入Calculate的calc方法是求两个数中大的,而Math的max方法正好是这个功能
         //所以可以使用Math::max 即类::静态方法名
        Calculate c = Math::max;
        int max = c.calc(9,900);
        System.out.println(max);
    }
}

@FunctionalInterface
interface Calculate{
    public int calc(int a,int b);
}

@FunctionalInterface
interface Str{
    public String toUpper(String str);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值