当前位置:网站首页>Lamda expression
Lamda expression
2022-06-26 05:59:00 【Mr.Rop】
/* deduction Lambda expression */
public class TestLambda {
// 3. Static inner class
static class Like2 implements ILike{
@Override
public void Lambda() {
System.out.println("I Like Lambda2");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.Lambda();
like = new Like2();
like.Lambda();
// 4. Local inner classes
class Like3 implements ILike{
@Override
public void Lambda() {
System.out.println("I Like Lambda3");
}
}
like = new Like3();
like.Lambda();
// 5. Anonymous inner class There is no class name , You have to use an interface or a parent class
like = new ILike() {
@Override
public void Lambda() {
System.out.println("I Like Lambda4");
}
};
like.Lambda();
// 6. use Lambda simplify
like = () -> {
System.out.println("I Like Lambda5");
};
like.Lambda();
}
}
// 1. Define a functional interface
interface ILike{
void Lambda();
}
// 2. Implementation class
class Like implements ILike{
@Override
public void Lambda() {
System.out.println("I Like Lambda");
}
}
practice
public class TestLambda2 {
static class Love2 implements ILove{
@Override
public void Love(int a) {
System.out.println("I Love You--->"+a);
}
}
public static void main(String[] args) {
ILove love = new Love();
love.Love(2);
love = new Love2();
love.Love(3);
class Love3 implements ILove{
@Override
public void Love(int a) {
System.out.println("I Love You--->"+a);
}
}
love = new Love3();
love.Love(4);
love = new ILove() {
@Override
public void Love(int a) {
System.out.println("I Love You--->"+a);
}
};
love.Love(5);
love = (int a) -> {
System.out.println("I Love You--->"+a);
};
love.Love(7);
// simplify 1
love = (a) -> {
System.out.println("I Love You--->"+a);
};
love.Love(8);
// simplify 2: Remove brackets
love = a -> {
System.out.println("I Love You--->"+a);
};
love.Love(9);
// simplify 3: Remove the curly braces
love = a ->
System.out.println("I Love You--->"+a);
love.Love(10);
// summary :
// lambda An expression can only be reduced to one line if it has one line of code , If there are many lines , Then wrap it in code blocks .
// The premise is that it must be a functional interface ( Functional interface : There is only one method in the interface )
// Multiple parameters can also be removed from the parameter type , If you want to get rid of everything , You have to put parentheses
}
}
interface ILove{
void Love(int a);
}
class Love implements ILove{
@Override
public void Love(int a) {
System.out.println("I Love You--->"+a);
}
}
边栏推荐
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
- Matching environment of ES6
- 【C語言】深度剖析數據在內存中的存儲
- 虚拟项目失败感想
- Posting - don't get lost in the ocean of Technology
- Customize WebService as a proxy to solve the problem of Silverlight calling WebService across domains
- 转帖——不要迷失在技术的海洋中
- tf. nn. top_ k()
- Spark source code analysis (I): RDD collection data - partition data allocation
- 从新东方直播来探究下小程序音视频通话及互动直播
猜你喜欢

Ribbon load balancing service call

小程序第三方微信授权登录的实现

Redis多线程与ACL

423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

Status mode, body can change at will

MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

Unicloud cloud development obtains applet user openid

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

类和对象的学习

家庭记账程序(第二版 加入了循环)
随机推荐
numpy. frombuffer()
【C語言】深度剖析數據在內存中的存儲
06. talk about the difference and coding between -is and = = again
Old love letters
卷妹带你学jdbc---2天冲刺Day2
Upgrading technology to art
SSH keygen specifies the path
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
机器学习 05:非线性支持向量机
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
Prototype mode, Baa Baa
5分钟包你学会正则表达式
The news of thunderbolt
状态模式,身随心变
Household accounting procedures (the second edition includes a cycle)
Day4 branch and loop
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Ribbon load balancing service call
Implementation of third-party wechat authorized login for applet
Feelings of virtual project failure