Java中的构造函数引用是什么?
甲Çonstructor 参考 就像一个方法 参考 不同之处在于方法的名称为“新的”。可以通过以下语法使用“类名”和关键字“new”来创建它。
语法
<Class-Name> :: new
在下面的示例中,我们使用java.util.function.Function。它是一个功能接口,其单个抽象方法是apply()。功能 接口 表示采用单个参数的操作Ť并返回结果[R。
示例
import java.util.function.*;
@FunctionalInterfaceinterface MyFunctionalInterface {
Employee getEmployee(String name);
}
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ConstructorReferenceTest {
public static void main(String[] args) {
MyFunctionalInterface mf = Employee :: new; // constructor reference
Function<String, Employee> f1 = Employee :: new; // using Function interface
Function<String, Employee> f2 = (name) -> new Employee(name); // Lambda Expression
System.out.println(mf.getEmployee("Raja").getName());
System.out.println(f1.apply("Adithya").getName());
System.out.println(f2.apply("Jaidev").getName());
}
}输出结果
Raja Adithya Jaidev
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短