Grant me the serenity to accept the things that cannot change,the courage to change the things I can,and the wisdom to know the difference
—— 24.6.20
一、方法引用的介绍
1.概述:引用方法
2.使用:
a.被引用的方法要写在重写方法里面
b.被引用的方法从参数上,返回值上要和所在重写方法一致,而且引用的方法最好是操作重写方法的参数值的
c.干掉重写方法的参数;干掉->;干掉被引用方法的参数-> 将被引用方法的.改成::
import java.util.function.Consumer;
import java.util.stream.Stream;
public class Demo324Method {
public static void main(String[] args) {
Stream<String> stream = Stream.of("张三", "李四", "王五", "赵六", "田七");
/**
* accept为重写方法:参数类型为String,无返回值
accept方法里面有println方法:println参数类型为String,被引用的方法操作重写方法的返回值
println没有返回值
*/
// stream.forEach(new Consumer<String>() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// });
// stream.forEach(s -> System.out.println(s));
stream.forEach(System.out::println);
}
}
2.对象名 — 引用成员方法
① 使用对象名引用成员方法格式:
对象::成员方法名
② 需求:
函数式接口:Supplier
java.util.function.supplier<T>接口抽象方法:T get()。
用来获取一个泛型参数指定类型的对象数据。supplier接口使用什么泛型,就可以使用get方法获取一个什么类型的数据
import java.util.function.Supplier;
public class Demo325Method {
public static void main(String[] args) {
method(new Supplier<String>() {
/*
get为重写方法:无参的,
返回值为stringtrim方法在get中:无参的,
返回值为string考虑使用方法引用
*/
@Override
public String get() {
return " abc ".trim();
}
});
method(() -> " abc ".trim());
method( " abc "::trim);
}
public static void method(Supplier<String> supplier){
String s = supplier.get();
System.out.println("s = " + s);
}
}
3.类名 — 引用静态方法
类名--引用静态方法
格式:
类名::静态成员方法
import java.util.function.Supplier;
public class Demo326Method {
public static void main(String[] args) {
method(new Supplier<Double>() {
/**
* get是重写的方法,无参,返回值类型为double
* random():无参,返回值类型为double
* @return
*/
@Override
public Double get() {
return Math.random(); // 随机一个小数
}
});
method(()->Math.random());
method(Math::random);
}
public static void method(Supplier<Double> supplier) {
Double aDouble = supplier.get();
System.out.println("aDouble = " + aDouble);
}
}
4.类 — 构造引用
① 类--构造方法引用
格式:
构造方法名称::new
② 需求:
函数式接口:Function
java.util.function.Function<T,R>接口
抽象方法:
R apply(T t),根据类型T的参数获取类型R的结果。用于数类型转换
package S110MethodUse;
import S109Stream.Person;
import java.util.function.Function;
public class Demo327Method {
public static void main(String[] args) {
method(new Function<String, Person>() {
/**
* apply为重写方法:有一个String的参数,返回值类型为Person对象
* new Person(s):一个String参数的构造,类型为String,返回值类型Person类型
* @param s the function argument
* @return
*/
@Override
public Person apply(String s) {
return new Person(s);
}
},"张三");
method(s -> new Person(s),"李四");
method(Person::new,"王五");
}
public static void method(Function<String, Person> function,String name){
Person person = function.apply(name);
System.out.println(person);
}
}
5.数组 — 数组引用
格式:
数组的数据类型[]::new
int[]::new 创建一个int型的数组
double[]::new 创建于一个double型的数组
import java.util.function.Function;
public class Demo328Method {
public static void main(String[] args) {
method(new Function<Integer, int[]>() {
/**
* apply:重写的方法,参数为Integer型,返回值类型为int[]
* new int[integer]
* @param integer the function argument
* @return
*/
@Override
public int[] apply(Integer integer) {
return new int[0];
}
},10);
method(integer -> new int[integer],10);
method(int[]::new,10);
}
public static void method(Function<Integer,int[]> function,Integer len) {
int[] apply = function.apply(len);
System.out.println(apply.length);
}
}