1、给定一个导演Director 接口,内含唯- -的抽象方法makeMovie,且无参数、无返回值,使用lambda表达式在Test中完成调用。
在下面的代码中,请使用Lambda的省略格式调用invokeDirect 方法,打印输出“导演拍电影啦!”字样:
| package com.xxx; |
| |
| public interface Director { |
| abstract void makeMovie(); |
| } |
| |
| package com.xxx; |
| |
| public class Test { |
| public static void main(String[] args) { |
| invokeDirect(() -> { |
| System.out.println("导演拍电影啦!"); |
| }); |
| } |
| |
| private static void invokeDirect(Director director) { |
| director.makeMovie(); |
| } |
| } |
| |
2、给定一个计算器Calculator接口,内含抽象方法calc(减法),其功能是可以将两个数字进行相减,并返回差值。使用Lambda表达式在Test 中完成调用。
在下面的代码中,请分别使用Lambda的标准格式及省略格式调用invokeCalc 方法,完成130和120的相减计算:
| package com.xxx; |
| |
| public interface Calculator { |
| abstract int calc(int a,int b); |
| } |
| |
| package com.xxx; |
| |
| public class Test { |
| public static void main(String[] args) { |
| int i = invokeCalc(130, 120, (int num1, int num2) -> { |
| return num1 - num2; |
| }); |
| |
| System.out.println(i); |
| } |
| |
| private static int invokeCalc(int a, int b, Calculator calculator) { |
| return calculator.calc(a,b); |
| } |
| } |
| |
3、定义出一一个函数式接口MyInterface,使用FunctionalInterface
注解标识函数式接口,接口中的唯一抽象方法 void show(String s);
1)请使用Lambda表达式实现这个函数式接口,调用show方法实现的功能为:将字符串转换成字节数组打印这个字节数组内容
2)请使用匿名内部类对象实现这个函数式接口,调用show方法,实现的功能为:将字符串转成整数然后+ 10输出结果
3)请定义一一个接口的实现类重写show方法,调用show方法,实现的功能为:获取出字符串中的每一个字符, 字符之间使用”--” 进行拼接
| package com.xxx; |
| |
| @FunctionalInterface |
| public interface MyInterface { |
| void show(String s); |
| } |
| |
| package com.xxx; |
| |
| import java.util.Arrays; |
| |
| public class Test { |
| public static void main(String[] args) { |
| getShow("一个字符串",(String s) -> { |
| byte[] bytes = s.getBytes(); |
| for (byte aByte : bytes) { |
| System.out.println(aByte); |
| } |
| }); |
| } |
| |
| private static void getShow(String s, MyInterface myInterface){ |
| myInterface.show(s); |
| |
| } |
| } |
| |
| package com.xxx; |
| |
| import java.util.Arrays; |
| |
| public class Test2 { |
| public static void main(String[] args) { |
| showInfo("abcde", (String str) -> { |
| System.out.println(Arrays.toString(str.getBytes())); |
| }); |
| |
| showInfo("123", new MyInterface() { |
| @Override |
| public void show(String s) { |
| System.out.println(Integer.parseInt(s) + 10); |
| } |
| }); |
| |
| Impl impl = new Impl(); |
| impl.show("你好啊"); |
| |
| } |
| |
| public static void showInfo(String s, MyInterface mi) { |
| mi.show(s); |
| } |
| } |
| |
| package com.xxx; |
| |
| public class Impl implements MyInterface { |
| @Override |
| public void show(String s) { |
| char[] chars = s.toCharArray(); |
| |
| StringBuilder sb = new StringBuilder(); |
| for (char aChar : chars) { |
| sb.append(aChar).append("--"); |
| } |
| |
| System.out.println(sb.substring(0, sb.length() - 2)); |
| } |
| } |
| |
| package com.xxx; |
| |
| public class Test3 { |
| public static void main(String[] args) { |
| |
| String str = "字符串的每一个字符"; |
| |
| MyInterface mi = new Impl(); |
| |
| mi.show(str); |
| } |
| } |
| |
4、函数式接口
1)定义一个函数式接口CurrentTimePrinter,其中抽象方法void printCurrentTime(), 使用注解
@Functionallnterface
2)在测试类中 定义static void showLongTime(CurrentTimePrinter timePrinter),该方法的预期行为是使
用timePrinter打印系统当前毫秒值
3)测试 showLongTime(),通过lambda表达式完成需求
| package com.xxx; |
| |
| @FunctionalInterface |
| public interface CurrentTimePrinter { |
| void printCurrentTime(); |
| } |
| |
| package com.xxx; |
| |
| import java.util.Date; |
| |
| public class Test { |
| public static void main(String[] args) { |
| showLongTime(() -> { |
| |
| |
| System.out.println(System.currentTimeMillis()); |
| }); |
| } |
| |
| private static void showLongTime(CurrentTimePrinter timePrinter) { |
| timePrinter.printCurrentTime(); |
| } |
| } |
| |
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16828066.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步