【问题标题】:Java Generics Cannot Find Symbol ConfusionJava 泛型找不到符号混淆
【发布时间】:2021-04-10 19:43:12
【问题描述】:

我目前对我的任务感到困惑。我正在使用泛型并遇到错误,我不明白为什么会出现它。任何帮助将不胜感激!

这是错误:

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 类是当行 String nodeLabel = node.getLabel(); 时 Generic 的 N 参数将是什么叫做。这个类存在所以我知道在我的包或任何东西中找到这个类不是问题。此外,getLabel() 方法是公共的且可访问的,因此它不是不正确的访问修饰符等问题。我很确定它与泛型有关。我是否必须在 Graph 类的顶部执行 或类似的操作?

非常感谢!

【问题讨论】:

    标签: java debugging generics graph compiler-errors


    【解决方案1】:

    在您的代码中,N 扩展了 Object 类(请参阅错误),当没有为您的泛型定义显式超类时会发生这种情况。这意味着它只能访问该类的函数和变量。据我所知,Object 类没有定义名为getLabel() 的方法,因此您可能应该将类型参数限制为类似

    public class Graph<N extends ClassWithLabel, L> {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2012-09-13
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      相关资源
      最近更新 更多