当前位置:网站首页>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表达式,必须是(实现某个接口)的类的对象。
接口中的方法不止一个,只能使用 匿名内部类。
边栏推荐
- Memorizing byte order of big and small end
- [microservice]eureka
- 如何写好测试用例以及go单元测试工具testify简单介绍
- From bitmap to bloom filter, C # implementation
- Nacos安装指南
- Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
- Network in network (dolls)
- 利用burp精准定位攻击者
- find_circ详细使用指南
- [vscade] preview MD file
猜你喜欢
Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"
Ten thousand words explanation - mindarmour Xiaobai tutorial!
泰国安全又划算的支付方式
An article takes you to learn container escape
当Transformer遇见偏微分方程求解
05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
Memorizing byte order of big and small end
剑指 Offer 10- II. 青蛙跳台阶问题
Simple and fast digital network (network dolls in the network)
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
随机推荐
【Mysql】时间字段默认设置为当前时间
入侵痕迹清理
Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
CPU exception handling
Serial port debugging tool mobaxtermdownload
Alibaba cloud server purchase, basic configuration, (xshell) remote connection and environment building
com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x
How to easily describe the process of machine learning?
How to use Pinia (I) introduce Pinia into the project
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
Is the low commission free account opening channel safe?
国产框架MindSpore联合山水自然保护中心,寻找、保护「中华水塔」中的宝藏生命
剑指 Offer 10- II. 青蛙跳台阶问题
一篇文章带你学会容器逃逸
“message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
目前哪个证券公司炒股开户是最好最安全的?
大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
手机能开户炒股吗 网上开户炒股安全吗
数字格式化的 js 库
Concepts de base de données Oracle