我一直在用通用类表示进行练习,以了解它们的工作方式,而且我有一个理论上的问题。
我的通用类如下:
public class CustomCollector<T> {
private T t;
//getter, setter
}
我的陈述如下:
CustomCollector<ArrayList<Integer>> customCollectorA = new CustomCollector<>()
customCollectorA.setT(Arrays.asList(1, 3, 4));
//IntelliJ says Arrays.asList()gives back a List,
//but an ArrayList is needed
//but Arrays.asList() gives back a List, so the types are incompatible
CustomCollector customCollectorB = new CustomCollector<ArrayList<Integer>>();
customCollectorB.setT(Arrays.asList(1, 3, 4));
//IntelliJ has no problem with it
CustomCollector customCollectorC = new CustomCollector<List<Integer>>();
customCollectorC.setT(Arrays.asList(1, 3, 4));
//IntelliJ has no problem with it
CustomCollector<List<Integer>> customCollectorD = new CustomCollector<>();
customCollectorD.setT(Arrays.asList(1, 3, 4));
//IntelliJ has no problem with it
CustomCollector<ArrayList<Integer>> customCollectorE = new CustomCollector();
customCollectorE.setT(Arrays.asList(1, 3, 4));
//IntelliJ says it's an unchecked assignment, if I add <>, then
//the error message disappears
我想,如果我想了解这里发生的事情-除非这是IntelliJ错误-我必须再次了解在创建对象的新实例时正在发生的事情。因此,主题本身似乎很容易,但是我正在努力将我现有的知识与表示形式的IntelliJ结果进行协调。
我的问题如下:
谢谢。