java中的双冒号::

双冒号运算符语法,类名/对象名::方法名
静态方法的引用,引用该对象的方法,不赋值,做相应的操作:

public class demo {
    public static void buildData(String data){
        System.out.println(data);
    }
    public static void main(String[] args) {
        List<String> list = Arrays.asList("a", "b", "c");
        list.forEach(demo::buildData);

    }
}

实例方法的引用:

public class demo {


    public static void main(String[] args) {

        Runnable runnable = () -> buildData("ancen", "ancen2");
        runnable.run();

        Runnable runnable1 = demo::noParamsRequired;
    }

    //需要调用带参数的
    public static void buildData(String value, String value2) {
        System.out.println(value + value2);
    }

    //不需要调用的参数
    public static void noParamsRequired() {
        System.out.println("no params required");
    }
}

构造函数的引用:

@Data
class Student {
    private Integer id;
    private String name;
    private String desc;

    public Student(String string) {
        System.out.println(string);
    }
}

public class demo {


    public static void main(String[] args) { 
        //studentFunction是Function类型的函数接口,接收一个字符串String,并返回Student对象
        Function<String,Student> studentFunction = Student::new;
        //通过方法构造一个Student对象
        Student student = studentFunction.apply("ancen");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值