我正在嘗試使用具有泛型類(lèi)型的 Java 函數(shù)式接口,但代碼無(wú)法編譯,我不知道為什么。這是代碼的簡(jiǎn)化版本:import java.util.ArrayList;import java.util.List;import java.util.function.Function;public class TestClass <T extends Number> { Function<List<T>, Integer> fun; public void testFun (List<T> x) { List<Number> ln = new ArrayList<>(); fun.apply(ln); // error: incompatible types: List<Number> cannot be converted to List<T> testApply(ln); // This is similar to the code above, but compiles fine } public <M extends Number> Integer testApply(List<M> x) { return 0; }}錯(cuò)誤在代碼中指出。關(guān)于我做錯(cuò)了什么的任何指示?
使用具有功能接口的 Java 泛型類(lèi)型時(shí)編譯錯(cuò)誤
慕標(biāo)琳琳
2021-11-03 17:00:15