我编写了以下代码来Lazy<T>
在Java中进行模拟:
import java.util.function.Supplier;
public class Main {
@FunctionalInterface
interface Lazy<T> extends Supplier<T> {
Supplier<T> init();
public default T get() { return init().get(); }
}
static <U> Supplier<U> lazily(Lazy<U> lazy) { return lazy; }
static <T>Supplier<T> value(T value) { return ()->value; }
private static Lazy<Thing> thing = lazily(()->thing=value(new Thing()));
public static void main(String[] args) {
System.out.println("One");
Thing t = thing.get();
System.out.println("Three");
}
static class Thing{ Thing(){System.out.println("Two");}}
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下警告:
"
(T)
Main中的值不能应用于(com.company.Main.Thing
)原因:没有类型变量T的实例存在,因此Supplier<T>
符合Lazy<Thing>
"
你能帮我找出问题所在吗?提前致谢!