Java8 中引入办法援用新个性,用于简化利用对象办法的调用, 办法援用是用来间接拜访类或者实例的曾经存在的办法或者构造方法。 办法援用提供了一种援用而不执行办法的形式,它须要由兼容的函数式接口形成的指标类型上下文。

计算时,办法援用会创立函数式接口的一个实例。 当Lambda表达式中只是执行一个办法调用时,不必Lambda表达式,间接通过办法援用的模式可读性更高一些。办法援用是一种更简洁易懂的Lambda表达式。

4.1、根本格局

办法援用应用一对冒号 :: 来简化对象办法的调用,当你想要应用办法援用时,指标援用放在分隔符 :: 前,办法名称放在前面, 如下模式:

办法援用参考示例:

4.2、办法援用分类

Java8 中对于办法援用次要分为三大类:

  • 结构器援用 Class::new
  • 静态方法援用 Class::static_method
  • 特定对象的办法援用 instance::method
4.2.1、结构器援用

语法是Class::new,或者更个别的Class< T >::new实例如下

借助结构器援用实例化Iphone 对象,代码如下:

public class IPhone {
private Integer id;
private String version;
private Date createTime;
private String name;

public IPhone() {
}

public IPhone(Integer id) {
this.id = id;
}

public IPhone(Integer id, String name) {
this.id = id;
this.name = name;
}
...
}


public static void main(String[] args) {
/**

  • 结构器援用
  • 无参结构器

*/
// 实现Supplier 接口 通过结构器援用
Supplier<IPhone> factory01= IPhone::new;
IPhone p01 = factory01.get();
System.out.println(p01);

/**

  • 等价的Lambda 写法

*/
Supplier<IPhone> factory02 = ()->new IPhone();
IPhone p02 = factory02.get();
System.out.println(p02);

/**

  • 当结构器办法存在参数 参数个数为1个时

*/
Function<Integer,IPhone> factory03 = IPhone::new;
IPhone p03 = factory03.apply(2019);
System.out.println(p03);

/**

  • 等价的Lambda 写法

*/
Function<Integer,IPhone> factory04 = (id)-> new IPhone(id);
IPhone p04 = factory04.apply(2019);
System.out.println(p04);

/**

  • 当结构器办法存在参数 参数个数为2个时

*/
BiFunction<Integer,String,IPhone> factory05 = IPhone::new;
IPhone p05 = factory05.apply(2019,"iphoneX");
System.out.println(p05);

/**

  • 等价的Lambda 写法

*/
BiFunction<Integer,String,IPhone> factory06 = (id,name)->new IPhone(id,name);
IPhone p06 = factory06.apply(2019,"iphoneMax");
System.out.println(p06);
/**
当结构器参数参过2个时怎么解决呢???
**/
}

4.2.2、静态方法援用

语法是Class::static_method,实例如下:

应用静态方法援用 执行IPhone 静态方法

public class IPhone {
private Integer id;
private String version;
private Date createTime;
private String name;

public IPhone() {
}

public IPhone(Integer id) {
this.id = id;
}

public IPhone(Integer id, String name) {
this.id = id;
this.name = name;
}


/**
静态方法
*/
public static void info(){
System.out.println("这是一部IPhone");
}
}

/**

  • 定义函数式接口

*/
@FunctionalInterface
public interface PrintFunction{
void print();
}

// 静态方法援用
PrintFunction pf01= IPhone::info;
pf01.print();
/**

  • 等价的Lambda 写法

*/
PrintFunction pf02 = () -> {
IPhone.info();
};
pf02.print();


// 静态方法援用 静态方法存在参数时
/**

  • 定义函数式接口

*/
@FunctionalInterface
public interface PrintFunction02<T,R> {
R print(T t);
}

/**

  • 静态方法援用 办法存在参数时

*/
PrintFunction02<String,Double> pf03 = IPhone::getPrice;
System.out.println(pf03.print("iphone"));

/**

  • 等价的Lambda 写法

*/
PrintFunction02<String,Double> pf04 =(str)->{
return IPhone.getPrice(str);
};

4.2.3、特定类的任意实例化对象的办法援用

语法是instance::method ,此时援用办法时必须存在实例,示例代码如下:

/**

  • 结构器援用 实例化对象
  • 成员办法援用

*/
BiFunction<Integer,String,IPhone> factory07= IPhone::new;
IPhone p07 = factory07.apply(2019,"iphoneX");
PrintFunction pp= p07::mm;
pp.print();

/**

  • 等价的Lambda 写法

*/
BiFunction<Integer,String,IPhone> factory08 = (id,name)-> newIPhone(id,name);
IPhone p08 = factory08.apply(2019,"iphoneMax");
PrintFunction pp02 = ()->{
p08.mm();
};
pp02.print();