我不明白为什么 Java 编译器会在以下情况下给我“未经检查的转换”警告:
我有这堂课:
public class NodeTree<T> {
T value;
NodeTree parent;
List<NodeTree<T>> childs;
NodeTree(T value, NodeTree parent) {
this.value = value;
this.parent = parent;
this.childs = null;
}
public T getValue() { return value; }
public void setValue(T value) { this.value = value; }
public NodeTree getParent() { return parent; }
public void setParent(NodeTree parent) { this.parent = parent; }
public List<NodeTree<T>> getChilds() {
if (this.childs == null) {
this.childs = new LinkedList<NodeTree<T>>();
}
return this.childs;
}
}
在主要课程中,我有以下说明:
NodeTree node = new NodeTree<Integer>(10, null);
NodeTree<Integer> child = new NodeTree<Integer>(20, node);
List<NodeTree<Integer>> childs = node.getChilds();
childs.add(child);
我无法解释为什么我会在这种类型的getChilds()行上收到警告:
warning: [unchecked] unchecked conversion
List<NodeTree<Integer>> childs = node.getChilds();
^
required: List<NodeTree<Integer>>
found: List
1 warning
getChilds()函数不返回 List 类型,它返回 List < NodeTree < T >> 类型。
请帮我理解。