5월 10일 내용정리
1.람다식(Lambda) : 인터페이스 변수에 대입
인터페이스의 익명 구현 객체를 생성
인터페이스는 직접 객체를 생성할 수 없음
구현 클래스를 이용해서 객체를 작성하는데 => 익명 구현 클래스를 생성하고 객체화한다.
인터페이스 변수 = 람다식;
2.타켓 타입(target Type) : 람다식이 대입되는 인터페이스
익명 구현 객체를 만들 때 사용할 인터페이스
인터페이스 변수 = 람다식;
3.함수적 인터페이스 : 하나의 추상 메서드만 선언된 인터페이스가 타겟타입
4.@FunctionalInterface : 하나의 추상 메서드만 가진 인터페이스 인지 컴파일러가 체크 왜냐면 두개이상 메서드가 있으면 어떤 메서드의 람다식을 구현 했는지 모르기 때문.
두 개 이상의 추상 메서드가 선언되어 있으면 컴파일 오류 발생
익명 구현 객체 생성
인터페이스 변수 = new 인터페이스(){
//인터페이스의 추상 메서드를 선언
1.필드
2.메소드
}
5.하나의 실행문만 있다면 {}를 생략할 수 있음
6.매개 변수가 하나일 경우에는 ()를 생략 가능
단, 매개변수가 아무것도 없을때는 ()를 써야함!
7.return문 있을 경우 중괄호{}와 return도 생략 가능
8.{}안에 return문만 있고, return문 뒤에 연산식이나 메서드 호출이 오는 경우엔 {}와 return 생략가능
package study_0510;
@FunctionalInterface
public interface ramda {
// public void method();
// public void method01(int x);
// public int method01(int x, int y);
}
==================================================
package study_0510;
public class ramdaTest {
public static void main(String[] args) {
// 매개값 없을때, 인터페이스에 -> 구현시 public void method();
// ramda myramda;
// myramda = () ->{
// String str="메서드 호출1";
// System.out.println(str);
// };
// myramda.method();
//
// myramda=()->{System.out.println("메서드 호출2");};
// myramda.method();
//
// myramda=()->System.out.println("메서드 호출3");
// myramda.method();
// 매개값 1개 일때, 인터페이스에 -> public void method01(int x);
// ramda myramda;
// myramda=(x)->{
// int result=x+20;
// System.out.println(result);
// };
// myramda.method01(5);
//
// myramda=x->System.out.println(x+20);
// myramda.method01(5);
// 매개값 2개 일때, 인터페이스에 -> public int method01(int x, int y);
// ramda myramda;
// myramda=(x, y) -> {
// int result01 = x + y;
// return result01;
// };
// System.out.println(myramda.method01(5, 10));
//
// myramda=(x, y) -> {
// return x + y;
// };
// System.out.println(myramda.method01(5, 10));
//
// myramda=(x, y) -> x + y;
// System.out.println(myramda.method01(5, 10));
}
}