2022-08-11 18:10:37 +08:00
|
|
|
|
## 方法引用
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
[TOC]
|
|
|
|
|
|
|
|
|
|
### 介绍
|
|
|
|
|
|
|
|
|
|
+ 熟悉lambda表达式才可以进行本章学习
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
|
|
|
|
+ 若 **lambda体** 中的内容有方法已经实现了,我们可以使用 “方法引用” (可以理解为方法引用是 lambda 表达式的另外一种表现形式)
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
### 用法
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
|
|
|
|
+ 通过对象名引用成员方法 对象 :: 成员方法名
|
|
|
|
|
+ 通过类名访问静态方法 类 :: 静态方法名
|
|
|
|
|
+ 通过super调用父类的成员方法 super :: 成员方法名
|
|
|
|
|
+ 通过this调用本类的成员方法 this :: 成员方法名
|
|
|
|
|
+ 构造器引用 类名 :: new
|
|
|
|
|
+ 数组引用 Type[] :: new
|
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
### 案例演示
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
#### 对象 :: 成员方法名
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
+ ##### 示例
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
```java
|
|
|
|
|
public class TestA{
|
|
|
|
|
private void consumer(String name,Consumer<String> consumer){
|
|
|
|
|
consumer.accept(name);
|
|
|
|
|
}
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
@Test
|
|
|
|
|
public void test5(){
|
|
|
|
|
FunctionRef functionRef = new FunctionRef();
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
consumer("大山",functionRef::outName);
|
|
|
|
|
consumer("小红",functionRef::outName);
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
class FunctionRef{
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
public void outName(String name){
|
|
|
|
|
System.out.println("名字:"+name);
|
|
|
|
|
}
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
+ ##### 输出内容
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
```
|
|
|
|
|
名字:大山
|
|
|
|
|
名字:小红
|
|
|
|
|
```
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
#### 类 :: 静态方法名
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
2022-08-11 18:10:37 +08:00
|
|
|
|
|
2022-08-11 23:08:20 +08:00
|
|
|
|
```java
|
|
|
|
|
public class TestA{
|
|
|
|
|
private void consumer(String name,Consumer<String> consumer){
|
|
|
|
|
consumer.accept(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test6(){
|
|
|
|
|
consumer("大山",FunctionRef::staticOutName);
|
|
|
|
|
consumer("小红",FunctionRef::staticOutName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class FunctionRef{
|
|
|
|
|
public static void staticOutName(String name){
|
|
|
|
|
System.out.println("名字:"+name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
名字:大山
|
|
|
|
|
名字:小红
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### super :: 成员方法名
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class TestA extends FunctionRef{
|
|
|
|
|
private void consumer2(String str,Consumer<String> consumer){
|
|
|
|
|
consumer.accept(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test7(){
|
|
|
|
|
consumer(" 大山 ",super::trim);
|
|
|
|
|
consumer(" 小红 ",super::trim);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class FunctionRef{
|
|
|
|
|
public void trim(String str){
|
|
|
|
|
System.out.println(str.trim());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
大山
|
|
|
|
|
小红
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### this :: 成员方法名
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class TestA{
|
|
|
|
|
public void trim(String str){
|
|
|
|
|
System.out.println(str.trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test8(){
|
|
|
|
|
consumer(" 大山 ",this::trim);
|
|
|
|
|
consumer(" 小红 ",this::trim);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
大山
|
|
|
|
|
小红
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 类名 :: new
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class TestA{
|
|
|
|
|
private void supplier2(Supplier<FunctionRef> refSupplier){
|
|
|
|
|
refSupplier.get().printName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void funcation2(String name,Function<String,FunctionRef> function){
|
|
|
|
|
function.apply(name).printName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test9(){
|
|
|
|
|
supplier2(FunctionRef::new);
|
|
|
|
|
|
|
|
|
|
funcation2("大山",FunctionRef::new);
|
|
|
|
|
funcation2("小红",FunctionRef::new);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class FunctionRef{
|
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
|
|
public FunctionRef(){
|
|
|
|
|
System.out.println("使用无参构造方法创建对象");
|
|
|
|
|
name="NULL";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FunctionRef(String name){
|
|
|
|
|
System.out.println("使用有参构造方法创建对象");
|
|
|
|
|
this.name=name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void printName() {
|
|
|
|
|
System.out.println("名字:"+name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
使用无参构造方法创建对象
|
|
|
|
|
名字:NULL
|
|
|
|
|
使用有参构造方法创建对象
|
|
|
|
|
名字:大山
|
|
|
|
|
使用有参构造方法创建对象
|
|
|
|
|
名字:小红
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Type[] :: new
|
|
|
|
|
|
|
|
|
|
+ ##### 示例
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class TestA{
|
|
|
|
|
@SafeVarargs
|
|
|
|
|
private final <R> R[] intFunction(IntFunction<R[]> function, R... rs){
|
|
|
|
|
R[] apply = function.apply(rs.length);
|
|
|
|
|
System.arraycopy(rs, 0, apply, 0, rs.length);
|
|
|
|
|
return apply;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void test10(){
|
|
|
|
|
|
|
|
|
|
System.out.println(Arrays.toString(intFunction(String[]::new, "叶凡", "萧炎")));
|
|
|
|
|
|
|
|
|
|
System.out.println(Arrays.toString(intFunction(Integer[]::new, 120, 130)));
|
|
|
|
|
|
|
|
|
|
System.out.println(Arrays.toString(intFunction(Integer[]::new)));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
+ ##### 输出内容
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
[叶凡, 萧炎]
|
|
|
|
|
[120, 130]
|
|
|
|
|
[]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> 示例主要用于演示用法,可能并不适合实际开发场景
|