본문 바로가기

Java

[Java] Lambda function 예제

반응형



/*
 * 메서드 vs 함수 
 * 메서드 :클래스 내에 있는 함수 
 * 함수 : 클래스 필요없이 단독 실행 가능한 메서드 
 */

@FunctionalInterface
interface Lambda{
    int sum(int a, int b); //(d, b)
}

/*
 * 
 * (매개변수) ->실행
 * public class Test{
 *      
 */

class Test{
    public int testFunc(Lambda lambda) {
        //lambda.sum(3, 5);
        System.out.println("-============== ");
        return lambda.sum(3, 5);
    }
}

public class LambdaTestCode {


    /*
     * 람다식문
     * (매개변수 목록) -> {실행문}
     * 함수형 람다 를 사용하기 위해서 필요한것들 
     * 1. 인터페이스 
     * 2. 인페이스 구현체 
     * 
     */    

    public static void main(String[] args) {
        Test no = new Test();
        int a = no.testFunc(new Lambda() {
            @Override
            public int sum(int a, int b) {
                //System.out.println("a+b 는 노람다 : "+ a+b);
                return a+b;
            }
        });
        System.out.println("=====+A "+ a);



        Test yes = new Test();
        int result = yes.testFunc( (d,b) ->{
            return d+b;
        });

        System.out.println("=========RESUTL : " +result);

        }

    private static int addFunction(int a, int b) {
        return a>b? a:b;
    }

}
반응형

'Java' 카테고리의 다른 글

[Spring] 의존관계 주입 방법  (0) 2021.12.30
[Spring] Component  (0) 2021.12.29
[Spring] Configuration N SingleTon  (0) 2021.12.27
[Spring] SingleTon 단점  (0) 2021.12.27
[Spring] SingleTon  (3) 2021.12.24