메서드 대신 하나의 식으로 표현하는 것으로 익명함수
혹은 1회용 함수
라고도 한다.
interface ComputeTool {
public abstract int compute(int x, int y);
}
abstract class AbsComputeTool {
public abstract int compute(int x, int y);
}
public class Test {
public static void main(String[] args) throws IOException {
ComputeTool cTool1 = new ComputeTool() {
@Override
public int compute(int x, int y) {
return x + y;
}
};
System.out.println(cTool1.compute(1, 2));
// 람다식 -> 추상메서드가 1개보다 많을 경우 사용불가능하다.
ComputeTool cTool2 = (x, y) -> {return x + y;};
System.out.println(cTool2.compute(1, 2));
System.out.println(cTool2.compute(1, 2));
// 람다식은 추상클래스에 사용하지 못하고, 인터페이스에서 사용할 수 있다.
// AbsComputeTool cTool3 = (x, y) -> {return x + y;};
}
}