找不到符号变量... V 扩展了 class 中声明的对象

cannot find symbol variable ... V extends Object declared in class

我正在为学校练习Java,我现在遇到了麻烦。

这是我的 Graph.java 文件:

package graph;

public interface Graph<V>{
    public boolean hasEdge(V one, V two);
    public void addNode(V other);
    public void addEdge(V other);
}

这是我的未定向Graph.java 文件:

package graph.undirected;

import graph.*;
import java.util.*;

public class UndirectedGraph<V> implements Graph<V>{

    private HashMap<V,V> neighbourList;
    private TreeMap<V,V> prev;
    private TreeMap<V,Integer> dist;

    public UndirectedGraph(){
        neighbourList = new HashMap<V,V>();
        prev = new TreeMap<V,V>();
        dist = new TreeMap<V,Integer>();
    }

    public boolean hasEdge(V one, V two){
        if(!(this.neighbourList.containsKey(one) && this.neighbourList.containsKey(two))){
            throw new java.util.NoSuchElementException("Nonexistent node.");
        }
        else{
            if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){
                return false;
            }
            return true;
        }
    }
    public void addNode(V other){
        if(!(this.neighbourList.containsKey(other))){
            // some code will come here
        }
    }
    public void addEdge(V other){
        if(!(this.neighbourList.containsKey(other))){
            // and some code will come here too
        }
    }
}

我收到以下错误:

graph\undirected\UndirectedGraph.java:23: error: cannot find symbol if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^ symbol: variable neighbourList location: variable one of type V where V is a type-variable: V extends Object declared in class UndirectedGraph graph\undirected\UndirectedGraph.java:23: error: cannot find symbol if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){ ^ symbol: variable neighbourList location: variable two of type V
where V is a type-variable: V extends Object declared in class UndirectedGraph 2 errors

我被困在这里。谁能帮帮我?

onetwo 属于 V 类型,在您的示例中出于所有目的都是 Object。这种类型 V 没有 neighbourList 字段,所以你不能在下面写,因为它不编译:

if( one.neighbourList.containsKey(two) && two.neighbourList.containsKey(one) ){