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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短