格式:
类名::静态方法
对象::成员方法
类名::new //类名引用构造方法
数组类型[ ]::new //数组引用构造方法
前提:lambda表达式,只有一行执行语句,并且调用的方法是已经知道了的方法
1、类名::静态方法, 其中对象::成员方法和这个方法类似,所以就不补充了
public static void main(String[] args) {
method(new Supplier(){
@Override
public Double get(){
return Math.random();
}
}); //完整写出所有的重载方法
method(()->{return Math.random();}); //lambda表达式
method(()->Math.random()); //省略版的lambda表达式
method(Math::random);//方法引用的方式
//去掉了前面的()->这些流程,同时random后面的括号省去,无论是否有参数,都直接省略
}
public static Double method (Supplier s){
Double num=s.get();
return num;
}
2、类名::new //类名引用构造方法
Functionf1 = new Function () { @Override public Person apply(String name) { return new Person(name); } }; //完整版的重载 //省略版的lambda表达式 Function f2 = name -> new Person(name); //采用方法引用的表达式 Function f3 = Person::new;
3、数组类型[ ]::new //数组引用构造方法
Functionf1 = new Function () { @Override public int[] apply(Integer length) { return new int[length]; } }; //完整版的重载 //省略版的lambda表达式 Function f2 = length -> new int[length]; //采用方法引用的表达式 Function f3 =int[]::new; int[] arr=f3.apply(5); System.out.println(Arrays.toString(arr));



