java8-05-再探函数式接口

 
1.自定义函数式接口  MyFun
 
   传入一个参数    返回一个参数
 
2.定义方法 
 
  传入一个参数 n  并将自定义函数式接口MyFun   也作为参数 
 
3.在定义的方法中调用该函数式的getValue()方法    传入一个参数 n
 
 
4.main方法调用    对100  做加减乘除
结果
 
1 package com.wf.zhang.java8.function;
2 
3 @FunctionalInterface
4 public interface MyFun {
5 
6     public Integer getValue(Integer num);
7 
8 }
MyFun
 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 }
Test
 
 

猜你喜欢

转载自www.cnblogs.com/wf-zhang/p/11809474.html
今日推荐