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 婆婆高寿祝福语大全简短