看到这个程序,我在Java 8中使用了Lambda和@FunctionalInterface
。即使我删除@FunctionalInterface
注释也很有效,所以我很困惑是否有必要标记@FunctionalInterface??
界面?为什么某些内部接口标记为@FunctionalInterface
public interface CalcInterface<N, V> {
V operation(N n1, N n2);
}
public static class NumberOperation<N extends Number, V extends Number> {
private N n1;
private N n2;
public NumberOperation(N n1, N n2) {
this.n1 = n1;
this.n2 = n2;
}
public V calc1(CalcInterface<N, V> ci) {
V v = ci.operation(n1, n2);
return v;
}
}
public static void main(String[] args) {
NumberOperation<Integer, Integer> np = new NumberOperation(13, 10);
System.out.println(np.calc1((n1, n2) -> n1 + n2));
System.out.println(np.calc1((n1, n2) -> n1 * n2));
}
{{1}}