上節(jié)我們學(xué)習(xí)了解了方法引用,今天我們接著學(xué)構(gòu)造器引用摇肌,廢話不多說(shuō),直接懟代碼
1.示例代碼
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
2.方法測(cè)試類(lèi)以及對(duì)應(yīng)代碼片段的解釋
public class Test {
public static void main(String[] args) {
/**
* 構(gòu)造引用:構(gòu)造器的參數(shù)列表引镊,需要與函數(shù)式接口中參數(shù)列表保持一致朦蕴!
* 類(lèi)名 :: new
* 注意:接口的抽象方法的參數(shù)和返回值必須和實(shí)例方法的參數(shù)和返回值類(lèi)型保持一致
*/
Supplier<Employee> sup = () -> new Employee(); //普通的lambda表達(dá)式
System.out.println(sup.get());
-----------------------------------------------------------------------
Supplier<Employee> sup2 = Employee::new; //lambda方法引用方式
System.out.println(sup2.get());
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者