s-day07-Lambda的格式和前提条件

This commit is contained in:
2026-08-08 10:07:42 +08:00
parent ed4557c932
commit 6b76e9333a
2 changed files with 32 additions and 0 deletions
@@ -17,6 +17,28 @@ package com.inmind.lambda01;
我们只是要编写run方法中的方法体而已。 我们只是要编写run方法中的方法体而已。
我们可以这么理解,函数式编程思想:做什么就是要重写的方法的方法体,而之前的对象创建,重写方法的语法都省略掉 我们可以这么理解,函数式编程思想:做什么就是要重写的方法的方法体,而之前的对象创建,重写方法的语法都省略掉
-----------------------------------------------------------------------
Lambda的格式和前提条件
函数式编程思想:重点关注做什么,而不是怎么做,在这个概念中做什么实际上就是一个方法的方法体
针对一个方法而言,它有3个核心:1.参数列表 2.方法体 3.返回值
Lambda表达式所需的语法的符号:(),->,{}
():参数列表
->:参数列表用来调用哪段代码
{}:方法体,根据是否有返回值,动态添加return
Lambda的格式:
(参数列表)->{
方法体;
[return]
}
lambda表达式的前提条件:
1.需要一个有且仅有一个抽象方法的接口(函数式接口)
2.接口作为参数或者变量类型
*/ */
public class Demo01 { public class Demo01 {
public static void main(String[] args) { public static void main(String[] args) {
@@ -27,5 +49,9 @@ public class Demo01 {
System.out.println(Thread.currentThread().getName()+"线程启动了"); System.out.println(Thread.currentThread().getName()+"线程启动了");
} }
}).start(); }).start();
//---------------------------------使用lambda表达式(函数式编程思想)优化以上代码---------------------------------
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"线程启动了");
}).start();
} }
} }
@@ -0,0 +1,6 @@
package com.inmind.lambda01;
@FunctionalInterface
public interface MyInterface {
void method2();
}