// 일반 메서드
int max (int a, int b) {
return a > b ? a : b;
}
// max() 와 같은 역할을 하는 람다 표현식
(a, b) -> a > b ? a: b
// -------------------------------------------
// 일반메서드
void printVar(String name, int i) {
System.out.println(name + " = " + i);
}
// printVar() 와 같은 역할을 하는 람다식
(name, i) -> System.out.println(name + "=" + i)
때문에 오로지 단하나의 클래스를 상속받거나 단하나의 인터페이스만 구현 가능 (둘이상은 불가능)
class Example {
// 익명클래스 정의 형태
Object annyClass01 = new Object() { void methodEx() {}};
static Object annyClass01 = new Object() { void methodEx() {}};
void methodNormal(){
Object annyClass02 = new Object() { void methodEx() {}};
}
}
// 익명클래스 사용 X
public class NoAnny {
public static void main(String[] args) {
Button buttonStart = new Button("Start");
buttonStart.addActionListener(new **EventHandler**());
}
}
class **EventHandler** implements **ActionListener** {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent Occurred !!! ");
}
}
//---------------------------------------------------------------
// 익명클래스 사용시
public class YesAnny {
public static void main(String[] args) {
Button buttonStart = new Button("Start");
buttonStart.addActionListener(new **ActionListener**() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent Occurred !!! ");
}
});
}
}
//---------------------------------------------------------------
//---------------------------------------------------------------
interface **MyFunction** {
public abstract int max(int a, int b);
}
// ...생략 main()
MyFunction f = new **MyFunction**() {
@Override
public int max(int a, int b) {
return a > b ? a : b;
}
};
int maxNum = f.max(3, 50);
// ...생략
interface **MyFunction** {
public abstract int max(int a, int b);
}
// 익명의 클래스의 메서드
MyFunction f = new **MyFunction**() {
@Override
public int max(int a, int b) {
return a > b ? a : b;
}
};
// 해당 람다식은 위 익명 클래스의 객체와 동등하다.
MyFunction f = (int a, int b) -> a > b ? a : b;
// 호출
int maxNum = f.max(3, 50);
→ 따라서 인터페이스를 통해 람다식을 다루기로 결정
→ 람다식을 다루기 위한 인터페이스 : Functional Interface (함수형 인터페이스) 라고 한다.
**@FunctionalInterface**
interface MyFunction{
public abstract int max(int a, int b);
}
// 함수형 인터페이스를 참조변수로 사용 : MyFunction f
MyFunction f = () -> System.out.println("myMethod is occurred !!!");
@FunctionalInterface
interface MyFunction{
public abstract int max(int a, int b);
}
// --------------------------생략
// 다음과 같이 함수형 인터페이스가 어떤 메서드의 매개변수 형으로 선언되어있을때
void aMethod(MyFunction f) {
f.myMethod();
}
// --------------------------생략
MyFunction f = () -> System.out.println("myMethod is occurred !!!");
// 람다식을 참조하는 참조변수가 매개변수로 지정될 수 있다.
aMethod(f);
// 또는 다음과 같은 방식 역시 가능하다.
aMethod(() -> System.out.println("myMethod is occurred !!!"));
@FunctionalInterface
interface MyFunction{
public abstract int max(int a, int b);
}
// --------------------------생략
// 다음과 같이 함수형 인터페이스가 어떤 메서드의 매개변수 형으로 선언되어있을때
MyFunction myMethod() {
MyFunction f = () -> {};
// 람다식을 가리키는 참조변수를 return
return f;
// 또는 다음과 같은 방식 역시 가능
return () -> {}; // myMethod 함수의 리턴타입이 지정되어있으므로 가능
}