Java8 中引入方法引用新特性,用于简化应用对象方法的调用,方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法。方法引用提供了一种引用而不执行方法的方式,它需要由兼容的函数式接口构成的目标类型上下文。
计算时,方法引用会创建函数式接口的一个实例。 当 Lambda 表达式中只是执行一个方法调用时,不用 Lambda 表达式,直接通过方法引用的形式可读性更高一些。方法引用是一种更简洁易懂的 Lambda 表达式。
一、基本格式
方法引用使用一对冒号 ::
来简化对象方法的调用,当你想要使用方法引用时,目标引用放在分隔符 ::
前,方法名称放在后面, 如下形式:
[图片上传失败...(image-84fa8a-1596556340340)]
方法引用参考示例:
image二、方法引用分类
Java8 中对于方法引用主要分为三大类:
-
构造器引用
Class::new
-
静态方法引用
Class::static_method
-
特定对象的方法引用
instance::method
最新技术学习资料(_) → lezijie007(程序员暗号999)
2.1、构造器引用
语法是Class::new
,或者更一般的Class< T >::new
实例如下
借助构造器引用实例化 Iphone 对象,代码如下:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n26" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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个时怎么解决呢???
**/
}</pre>
2.2、静态方法引用
语法是Class::static_method
,实例如下:
使用静态方法引用执行 IPhone 静态方法
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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);
};</pre>
2.3、特定类的任意实例化对象的方法引用
语法是instance::method
,此时引用方法时必须存在实例,示例代码如下:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/**
- 构造器引用 实例化对象
- 成员方法引用
/
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)-> new IPhone(id,name);
IPhone p08 = factory08.apply(2019,"iphoneMax");
PrintFunction pp02 = ()->{
p08.mm();
};
pp02.print();</pre>
网友评论