예제
@FunctionalInterface
public interface BinaryOp {
int apply(int a, int b);
}
public class Main {
public static void main(String[] args) {
BinaryOp add = (x, y) -> x + y;
System.out.println(add.apply(1, 2)); // 출력: 3
}
}
@FunctionalInterface
public interface BinaryOp {
int apply(int a, int b);
}
public class Something {
public static int add(int i, int j) {
return i + j;
}
}
public class Main {
public static void main(String[] args) {
BinaryOp op = Something::add;
System.out.println(op.apply(1, 2)); // 출력: 3
}
}
@FunctionalInterface
public interface BinaryOp {
int apply(int a, int b);
}
public class Main {
public static void main(String[] args) {
BinaryOp op = new BinaryOp() {
@Override
public int apply(int a, int b) {
return a + b;
}
};
System.out.println(op.apply(1, 2)); // 출력: 3
}
}
요약
따라서 FunctionalInterface를 직접 생성하는 것이 아니라, 이 인터페이스를 구현하는 방법으로 객체를 생성하고 사용할 수 있습니다.