在jdk8中是否需要@FunctionalInterface

时间:2017-09-06 01:29:20

标签: lambda java-8 functional-interface

看到这个程序,我在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}}

0 个答案:

没有答案