3
我這樣做代碼:不兼容的泛型類型的Java
import java.util.LinkedList;
public class Node<T> {
private T data;
private LinkedList<T> children;
public Node(T data) {
this.data = data;
this.children = new LinkedList<T>();
}
public T getData() {
return this.data;
}
public LinkedList<T> getChildren(){
return this.children;
}
}
public class Graph <T> implements Network<T> {
private Node source;
private Node target;
private ArrayList<Node> nodes = new ArrayList<Node>();
public Graph(T source,T target) {
this.source = new Node(source);
this.target = new Node(target);
}
public T source() {
return source.getData();
}
public T target() {
return target.getData();
}
我得到源()和目標這個錯誤():需要t上找不到java.lang.Object繼承爲什麼呢?的getData()函數的返回類型是T(通用型回報)
更換
Node
與Node<T>
可能是重複的http://計算器。 com/questions/4121179/casting-objects-to-t-type – Avi編譯器應該如何知道它們是相同的'T'?你必須明確地聲明它:'節點源;節點目標;' –
Obicere
@Obicere感謝它的工作! – HKing