【发布时间】:2021-01-07 23:25:40
【问题描述】:
在 代码 1: 中没有错误并且可以正常工作。 代码 2 有提到的编译时错误。
问题 1: 为什么?
问题 2: 我们如何在代码 2 中返回一个函数?
代码 1:
static <T> Consumer<T> m1(Consumer<T> consumer) {
Consumer<T> c = obj -> {
consumer.accept(obj);
};
return c;
}
代码 2:
static <T, R> Function<T, R> m2(Function<T, R> f) {
// Compile Error: The target type of this expression must be a functional interface
Function<T, R> o = {x -> {
f.apply(x);
}};
return o;
}
【问题讨论】:
标签: java eclipse generics lambda