【发布时间】:2019-06-24 18:44:14
【问题描述】:
- 当功能接口位于 lambda 覆盖它的同一文件中时,它可以正常编译。
package test.test;
public class Base {
public static void main(String[] args) {
Interface1 a = n -> System.out.println(2*n);
}
}
interface Interface1 {
void multiplyByTwo(int x);
}
- 当功能接口在单独的文件中并且基类实现它时,编译失败并出现
Base is not abstract and does not override abstract method multiplyByFour(int) in Interface3错误。
package test.test;
public class Base implements Interface3 {
public static void main(String[] args) {
Interface3 b = n -> System.out.println(4*n);
}
}
package test.test;
public interface Interface3 {
void multiplyByFour(int x);
}
- 这里有什么问题吗?第二种情况下为什么lambda不覆盖方法?
【问题讨论】:
-
你不需要实现就可以使用
-
Base类不需要实现该类中的 lambda 匹配的接口。