在构造函数具有非空参数列表的情况下使用构造函数引用 [英] Use of constructor reference where constructor has a non-empty parameter list

查看:70
本文介绍了在构造函数具有非空参数列表的情况下使用构造函数引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给..

List<Foo> copy(List<Foo> foos) {
    return foos
            .stream()
            .map(foo -> new Foo(foo))
            .collect(Collectors.toList());
}

IntelliJ IDEA 2016.1.1报告new Foo(foo)可以用方法引用替换".

IntelliJ IDEA 2016.1.1 reports that new Foo(foo) "can be replaced with method reference".

我知道no-arg构造函数的Foo::new语法,但是看不到如何将foo作为参数传递.我肯定在这里错过了一些东西.

I'm aware of the Foo::new syntax for the no-arg constructor, but don't see how I could pass foo in as an argument. I'm surely missing something here.

推荐答案

我知道no-arg构造函数的Foo::new语法

这不是Foo::new所做的. 此表达式将扩展到使用上下文时所需的表达式.

在这种情况下

List<Foo> copy(List<Foo> foos) {
    return foos.stream().map(Foo::new).collect(Collectors.toList());
}

将寻找需要Foo自变量的构造函数.

would look for a constructor that needed a Foo argument.

这篇关于在构造函数具有非空参数列表的情况下使用构造函数引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆