【发布时间】:2012-10-01 13:31:16
【问题描述】:
我在Graph<T> 类中有一个Node<T>(内部)类:
public class Graph<T> {
private ArrayList<Node<T>> vertices;
public boolean addVertex(Node<T> n) {
this.vertices.add(n);
return true;
}
private class Node<T> {...}
}
当我运行这个时:
Graph<Integer> g = new Graph<Integer>();
Node<Integer> n0 = new Node<>(0);
g.addVertex(n0);
最后一行给我错误:
The method addVertice(Graph<Integer>.Node<Integer>) in the type Graph<Integer> is not applicable for the arguments (Graph<T>.Node<Integer>)
为什么?先谢谢了?
【问题讨论】:
-
什么语言?我猜是 C#,但可能还有其他具有相同(或相似)语法的人。
-
你在哪里定义了addVertice?我们在示例中看不到它。
-
你应该将
Node类设为static。 -
@astander 我重新编辑了代码。抱歉错了>
-
那是因为如果它不是静态的,你需要一个外部类的对象来创建一个内部类的对象。