java8-05-再探函数式接口
1.自定义函数式接口 MyFun
传入一个参数 返回一个参数
2.定义方法
传入一个参数 n 并将自定义函数式接口MyFun 也作为参数
3.在定义的方法中调用该函数式的getValue()方法 传入一个参数 n
4.main方法调用 对100 做加减乘除
(可以理解为Lambda表达式是对函数式接口的具体实现)
![](https://img2018.cnblogs.com/i-beta/1445197/201911/1445197-20191107161750144-234331257.png)
结果
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.wf.zhang.java8.function; 2 3 @FunctionalInterface 4 public interface MyFun { 5 6 public Integer getValue(Integer num); 7 8 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.wf.zhang.java8.function; 2 3 public class Test { 4 5 6 public static void main(String[] args) { 7 8 Integer num1 = operation(100, t -> t * t); 9 Integer num2 = operation(100, t -> t - 10); 10 Integer num3 = operation(100, t -> t / 5); 11 Integer num4 = operation(100, t -> t + 88); 12 System.out.println(num1); 13 System.out.println(num2); 14 System.out.println(num3); 15 System.out.println(num4); 16 } 17 18 19 //定义方法 将自定义函数接口作为参数 20 public static Integer operation(int n, MyFun mf) { 21 return mf.getValue(n); 22 } 23 }
古人学问无遗力,少壮工夫老始成。
纸上得来终觉浅,绝知此事要躬行。