s-day07--函数式编程思想概述

This commit is contained in:
xx
2026-10-20 10:17:03 +08:00
parent cf844fba2f
commit 6d87914d2e
2 changed files with 44 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@@ -0,0 +1,33 @@
package com.inmind.lambda01;
/*
使用匿名内部类实现多线程
匿名内部类的语法:
new 父类|父接口(){
要重写的方法
}
线程的启动方式有2种:
1.继承Thread
2.实现Runnable接口(重点)
-----------------------------------------------
函数式编程思想:重点关注做什么,而不是怎么做
为了编写线程任务代码,我们必须面向对象,new Runnable(),再重写run()(public void run()),其实
我们只是要编写run方法中的方法体而已。
我们可以这么理解,函数式编程思想:做什么就是要重写的方法的方法体,而之前的对象创建,重写方法的语法都省略掉
*/
public class Demo01 {
public static void main(String[] args) {
//接口的匿名内部类的方式启动线程(面向对象的写法)
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程启动");
}
}).start();
//-----------------------以下使用函数式编程思想lambda表达式来优化线程代码----------------------------------
new Thread(()->{System.out.println(Thread.currentThread().getName()+"线程启动");}).start();
}
}