当前位置:网站首页>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);
}
}
边栏推荐
- Source code of findcontrol
- MySQL-05
- Selective search for object recognition paper notes [image object segmentation]
- Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast
- Redis多线程与ACL
- Cyclic displacement
- Unicloud cloud development obtains applet user openid
- The use of loops in SQL syntax
- C generic speed
- "= =" difference from "equals"
猜你喜欢

Easy to understand from the IDE, and then talk about the applet IDE

pytorch(网络模型)

状态模式,身随心变

Redis underlying data structure

Kolla ansible deploy openstack Yoga version

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

Pytorch (network model training)

工厂方法模式、抽象工厂模式

Unicloud cloud development obtains applet user openid

Class and object learning
随机推荐
DOM document
Old love letters
SQL Server视图
类和对象的学习
05. basic data type - Dict
E-commerce seeks growth breakthrough with the help of small program technology
C XX management system
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Written before father's Day
家庭记账程序(第一版)
The difference between overload method and override method
Life is so fragile
numpy. tile()
Matching environment of ES6
On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back
Level signal and differential signal
numpy. exp()
String类学习
numpy.tile()
【C語言】深度剖析數據在內存中的存儲