- package com.wcc.d_Lambda;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @Author kk
- * @Date 2020/3/12 16:39
- */
- /**
- * 函数式接口
- */
- @FunctionalInterface
- interface PrintList{
- /**
- * 方法参数为 list 的集合
- * 没有返回值
- * @param list List 集合
- */
- void print(List<?> list);
- }
- /**
- *
- */
- class MethodReference{
- /**
- * 该方法符合要求
- * 没有返回值
- * @param list List 集合
- */
- public static void showListInfo(List<?> list){
- for (Object o : list) {
- System.out.println(o);
- }
- }
- }
- /**
- * 通过类名来执行方法引用
- */
- public class Demo3 {
- public static void main(String[] args) {
- /*
- 明确调用对象
- 当前方法是一个静态方法, 需要通过类名调用
- 明确调用方法
- showListInfo
- 明确的数据
- ArrayList<String> list = new ArrayList<>();
- 可省略可推导
- */
- testClass(MethodReference::showListInfo);
- testClass(list -> {
- for (Object o : list) {
- System.out.println(o);
- }
- });
- }
- /**
- * 利用了一个函数式接口做欸方法的参数
- * @param printList 函数式接口
- */
- public static void testClass(PrintList printList){
- ArrayList<String> list = new ArrayList<>();
- list.add("BMW");
- list.add("Audi");
- printList.print(list);
- }
- }
通过 super 关键字执行方法引用
来源: http://www.bubuko.com/infodetail-3461126.html