public interface MyStudent {
Student build(String mane,int age);
}
/*引用构造器,其实就是引用构造方法
格式: 类名::new*/
public class Deom02 {
public static void main(String[] args) {
//lambda表达式
useMyStudent((mane, age) -> new Student(mane, age));
//构造器
useMyStudent(Student::new);
//lambda表达式被构造器替换时,它的参数全部传给构造器作为参数
}
private static void useMyStudent(MyStudent s) {
Student student = s.build("张三", 18);
System.out.println(student.getName() + "," + student.getAge());
}
}
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
Java新特性
最新推荐文章于 2024-08-29 20:29:58 发布