Supplier: 供给型接⼝:⽆⼊参,有返回值
T:出参类型;没有⼊参
调⽤⽅法:T get();
T:出参类型;没有⼊参
调⽤⽅法:T get();
@FunctionalInterface public interface Supplier<T> { T get(); }
⽤途: 泛型⼀定和⽅法的返回值类型是⼀种类型,如果需要获得⼀个数据,并且不需要传⼊参数,可
以使⽤Supplier接⼝,例如 ⽆参的⼯⼚⽅法,即⼯⼚设计模式创建对象,简单来说就是 提供者
以使⽤Supplier接⼝,例如 ⽆参的⼯⼚⽅法,即⼯⼚设计模式创建对象,简单来说就是 提供者
public class Main { public static void main(String[] args) { //Student student = new Student(); Student student = newStudent(); System.out.println(student.getName()); } public static Student newStudent(){ Supplier<Student> supplier = ()-> { Student student = new Student(); student.setName("默认名称"); return student; }; return supplier.get(); } static class Student{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } }
本文作者为DBC,转载请注明。