我无法正确使用此地图方法。我需要指定通用
Think<R>
但在哪里?
public class Think<T> implements Supplier<T> {
private Supplier<T> supplier;
public Think(Supplier<T> expression) {
this.supplier = () -> evaluate(expression);
}
public static <T> Think<T> of(T value) {
return new Think<>(() -> value);
}
private synchronized T evaluate(Supplier<T> expression) {
this.supplier = expression;
return this.supplier.get();
}
@Override
public T get() {
return this.supplier.get();
}
public <R> Think<R> map(Function<? super T, ? extends R> mapper) {
return Think.of(() -> mapper.apply(get()));
}
产量
incompatible types: cannot infer type-variable(s) T (argument mismatch; R is not a functional interface)
方法映射本身是正确的,但是当你调用Think.of()时就会出现问题。
更改 Think.of() 方法以接受供应商而不是 R 类型的直接值。
public static <T> Think<T> of(Supplier<T> supplier) {
return new Think<>(supplier);
}