当前位置:网站首页>Lambda表达式
Lambda表达式
2022-06-27 00:04:00 【Cold Snowflakes】
体验 lambda表达式
// 匿名内部类方式
new Thread (new Runnable(){
@Override
public void run() {
System.out.println("多线程程序启动了");
}
}).start();
// lambda表达式
new Thread(
() -> {
System.out.println("多线程程序启动了"); }
).start();
--------------------------------------------------------------
() -> {
System.out.println("多线程程序启动了"); }
// 这里的作用实际上就是代替了这部分:
new Runnable(){
@Override
public void run() {
System.out.println("多线程程序启动了");
}
}
实际上作用是代替了匿名内部类,只不过是实现某个接口的,没有继承某个类。
Lambda表达式的使用前提:
1.有一个接口。
2.接口中有且仅有一个抽象方法。
省略
参数的类型可以省略。
如果参数只有一个,小括号可以省略。
如果代码块的语句只有一条,可以省略大括号和语句的分号。如果这一条语句是 return语句,省略的时候,要把return也省略掉。
useFlyable(
s -> {
System.out.println(s); }
);
// 等价
useFlyable(
s -> System.out.println(s)
);
注意事项
1.使用Lambda必须要有接口,并且该接口中有且仅有一个抽象方法。
2.lambda表达式实际上是充当了一个接口的实现类的对象的角色,参数是抽象方法的参数,代码块是抽象方法的方法体。
3.必须有上下文环境,才可以推导出 Lambda 对应的接口
public static void main(String[] args) {
// 根据局部变量的赋值得知 Lambda 对应的接口
Runnable r = () -> System.out.println("Lambda表达式");
// 根据调用方法的参数得知 Lambda 对应的接口
new Thread(() -> System.out.println("Lambda表达式")).start();
// 方法参数必须是一个接口, 不可以是抽象类 或者 具体类。
}
和匿名内部类的区别
匿名内部类,可以是(实现某个接口,继承某个抽象类 / 具体类)的类的对象。
Lambda表达式,必须是(实现某个接口)的类的对象。
接口中的方法不止一个,只能使用 匿名内部类。
边栏推荐
- [test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories
- Why does EDR need defense in depth to combat ransomware?
- Concepts de base de données Oracle
- Batch generate folders based on file names
- 论文学习——降雨场次划分方法对降雨控制率的影响分析
- 2022健康博览会,山东养生保健展会,产后健康、睡眠健康展
- idea 热启动失效解决方案
- 泰国安全又划算的支付方式
- The fourth bullet of redis interview eight part essay (end)
- In the Internet industry, there are many certificates with high gold content. How many do you have?
猜你喜欢

Memorizing byte order of big and small end

CPU的异常处理

Sword finger offer 10- ii Frog jumping on steps

Technical dry goods | top speed, top intelligence and minimalist mindspore Lite: help Huawei watch become more intelligent

kubernetes可视化界面dashboard

国内外最好的12款项目管理系统优劣势分析

Lwip之ARP模块实现

Redis detailed tutorial

Can't write to avoid killing and can easily go online CS through defender

大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
随机推荐
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
Understanding of "the eigenvectors corresponding to different eigenvalues cannot be orthogonalized"
Introduction to software engineering -- Chapter 4 -- formal description technology
Deep learning method for solving mean field game theory problems
万字详解-MindArmour 小白教程!
Your connection is not private
Technical dry goods | top speed, top intelligence and minimalist mindspore Lite: help Huawei watch become more intelligent
CPU的异常处理
我的c语言进阶学习笔记 ----- 关键字
股票怎样在手机上开户安全吗 网上开户炒股安全吗
05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
网络中的网络(套娃)
[微服务]认识微服务
當Transformer遇見偏微分方程求解
国产框架MindSpore联合山水自然保护中心,寻找、保护「中华水塔」中的宝藏生命
Installation of xshell and xftp
[vscode] setting sync, a plug-in for synchronizing extensions and settings
Cvpr2022 stereo matching of asymmetric resolution images
Outside the code: writing is the best way to force growth