扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
主要的三种语法格式:
成都创新互联公司是一家专业提供历城企业网站建设,专注与成都做网站、成都网站制作、H5建站、小程序制作等业务。10年已为历城众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。注意:
若Lambda参数列表中的第一参数是 实例方法的调用者,而第二个参数是 实例方法的参数时,可以使用ClassName::method
public class Employee {
private String name;
private int age;
private double salary;
public Employee() {
super();
}
public Employee(int age){
this.age = age;
}
public Employee(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
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{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
//对象::实例方法名
@Test
public void test1(){
PrintStream ps1 = System.out;
Consumer con = (x) -> ps1.println(x);
PrintStream ps = System.out;
Consumer con1 = ps::println;
Consumer con2 = System.out::println;
con2.accept("abcdef");
}
@Test
public void test2(){
Employee employee = new Employee();
Supplier sup = () -> employee.getName();
String str = sup.get();
System.out.println(str);
Supplier sup2 = employee::getName;
String str2 = sup2.get();
System.out.println(str2);
}
//类:静态方法名
@Test
public void test3(){
Comparator com = (x, y) -> Integer.compare(x, y);
Comparator com1 = Integer::compare;
}
格式:ClassName::new
注意:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致
public class Employee {
private String name;
private int age;
private double salary;
public Employee() {
super();
}
public Employee(int age){
this.age = age;
}
public Employee(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
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{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
//构造器引用
@Test
public void test5(){
Supplier sup = () -> new Employee();
//构造器引用方式
Supplier sup2 = Employee::new;
Employee employee = sup2.get();
System.out.println(employee);
}
@Test
public void test6(){
Function fun = (x) -> new Employee(x);
Function fun2 = Employee::new;
Employee emp = fun2.apply(101);
System.out.println(emp);
}
格式:Type[ ]::new;
//数组引用
@Test
public void test7(){
Function fun = (x) -> new String[x];
String[] strs = fun.apply(10);
System.out.println(strs.length);
Function fun2 = String[]::new;
String[] strs2 = fun2.apply(20);
System.out.println(strs2.length);
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流