Java泛型找不到符号混淆
Java Generics Cannot Find Symbol Confusion
我目前对我的作业感到困惑。我正在使用 Generics 并遇到错误,我不明白为什么会出现错误。任何帮助将不胜感激!
这里是错误:
C:\path>javac *.java
Graph.java:76: error: cannot find symbol
String nodeLabel = node.getLabel();
^
symbol: method getLabel()
location: variable node of type N
where N is a type-variable:
N extends Object declared in class Graph
我在下面代码中发生此错误的行旁边添加了注释
相关方法如下:
public void propogate(N node, float lambda, Graph<N,L> otherGraph) {
//find degree of node
int theDegree = getDegreeOfNode(node);
//determine num of susceptible neighnbors to be infected
int numToInfect = -1;
for (int i = 0; i < theDegree + 1; i++) {
float calc = (newInfected + i) / infectNodesProc;
if (compare(calc, lambda) > 0) {
float val1 = calc - lambda;
float val2 = lambda - ((newInfected + (i - 1)) / infectNodesProc);
if (compare(val1, val2) < 0) {
numToInfect = i;
} else {
numToInfect = i - 1;
}
break;
}
}
if (numToInfect > 0) {
otherGraph.infectNeighbors(node, theDegree, numToInfect);
newInfected += numToInfect;
}
infectNodesProc++;
}
public void infectNeighbors(N node, int theDegree, int numToInfect) {
//get equivalent node in this graph
String nodeLabel = node.getLabel(); // THIS IS THE LINE THAT THE ERROR IS TALKING ABOUT
Iterator<N> it = nodes.iterator();
N theNode = null;
while (it.hasNext()) {
N aNode = it.next();
if (aNode.getLabel().equals(nodeLabel)) {
theNode = aNode;
break;
}
}
ArrayList<N> suscNeighbors = getSuscNeighbors(theNode);
int toInfect = numToInfect;
while (suscNeighbors.size() > 0 && toInfect > 0) {
Random rand = new Random();
int randInd = rand.nextInt(suscNeighbors.size());
N removedNode = suscNeighbors.remove(randInd);
removedNode.state = StateEnum.INFECTIOUS;
toInfect--;
}
}
上面的两个方法都在Graph里面class.
Node class 是当行 String nodeLabel = node.getLabel(); 时 Generic 的 N 参数。叫做。这个 class 存在所以我知道在我的包裹或任何东西中找到 class 不是问题。此外,getLabel() 方法是 public 并且可以访问,因此这不是访问修饰符不正确等问题。我很确定它与泛型有关。我是否必须在图 class 的顶部执行 或类似的操作?
非常感谢!
在您的代码中,N 扩展了对象 class(见错误),当没有为您的泛型定义明确的 superclass 时会发生这种情况。这意味着它只能访问 class 的函数和变量。据我所知,对象 class 没有定义一个名为 getLabel()
的方法,因此您可能应该将类型参数限制为
public class Graph<N extends ClassWithLabel, L> {
...
}
我目前对我的作业感到困惑。我正在使用 Generics 并遇到错误,我不明白为什么会出现错误。任何帮助将不胜感激!
这里是错误:
C:\path>javac *.java
Graph.java:76: error: cannot find symbol
String nodeLabel = node.getLabel();
^
symbol: method getLabel()
location: variable node of type N
where N is a type-variable:
N extends Object declared in class Graph
我在下面代码中发生此错误的行旁边添加了注释
相关方法如下:
public void propogate(N node, float lambda, Graph<N,L> otherGraph) {
//find degree of node
int theDegree = getDegreeOfNode(node);
//determine num of susceptible neighnbors to be infected
int numToInfect = -1;
for (int i = 0; i < theDegree + 1; i++) {
float calc = (newInfected + i) / infectNodesProc;
if (compare(calc, lambda) > 0) {
float val1 = calc - lambda;
float val2 = lambda - ((newInfected + (i - 1)) / infectNodesProc);
if (compare(val1, val2) < 0) {
numToInfect = i;
} else {
numToInfect = i - 1;
}
break;
}
}
if (numToInfect > 0) {
otherGraph.infectNeighbors(node, theDegree, numToInfect);
newInfected += numToInfect;
}
infectNodesProc++;
}
public void infectNeighbors(N node, int theDegree, int numToInfect) {
//get equivalent node in this graph
String nodeLabel = node.getLabel(); // THIS IS THE LINE THAT THE ERROR IS TALKING ABOUT
Iterator<N> it = nodes.iterator();
N theNode = null;
while (it.hasNext()) {
N aNode = it.next();
if (aNode.getLabel().equals(nodeLabel)) {
theNode = aNode;
break;
}
}
ArrayList<N> suscNeighbors = getSuscNeighbors(theNode);
int toInfect = numToInfect;
while (suscNeighbors.size() > 0 && toInfect > 0) {
Random rand = new Random();
int randInd = rand.nextInt(suscNeighbors.size());
N removedNode = suscNeighbors.remove(randInd);
removedNode.state = StateEnum.INFECTIOUS;
toInfect--;
}
}
上面的两个方法都在Graph
Node class 是当行 String nodeLabel = node.getLabel(); 时 Generic 的 N 参数。叫做。这个 class 存在所以我知道在我的包裹或任何东西中找到 class 不是问题。此外,getLabel() 方法是 public 并且可以访问,因此这不是访问修饰符不正确等问题。我很确定它与泛型有关。我是否必须在图 class 的顶部执行
非常感谢!
在您的代码中,N 扩展了对象 class(见错误),当没有为您的泛型定义明确的 superclass 时会发生这种情况。这意味着它只能访问 class 的函数和变量。据我所知,对象 class 没有定义一个名为 getLabel()
的方法,因此您可能应该将类型参数限制为
public class Graph<N extends ClassWithLabel, L> {
...
}