当前位置:网站首页>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);
    }
}
原网站

版权声明
本文为[Mr.Rop]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180503012199.html