如何在静态类中调用泛型类的方法? java的

时间:2012-11-17 23:30:30

标签: java generics static

我有4个类:edge,graph,node和shortestpath。我想在我的shortestpath类的static main中调用我的graph类中的方法。我得到的错误是“无法从类型图中对非静态方法readFile()进行静态引用”。我很感激任何帮助我卡住了:(!

public class edge<E>{}
public class node<E> extends edge<E>{}

public class graph<E> {
   public node<E> BFS(E value)
   {
    if (adjList.isEmpty())
        return (null);

    ArrayList<node<E>> visitedNodes = new ArrayList<node<E>>(); 

    node<E> sourceNode = adjList.get(0);
    sourceNode.setVisited(true);

    visitedNodes.add(sourceNode); 

    node<E> currNode = null; 

    while(!visitedNodes.isEmpty())
    {
        currNode = visitedNodes.get(0);
        visitedNodes.remove(0);

        if(currNode.getData() == value)
            return (currNode);

        //ListIterator<edge<E>> itr = currNode.incidentEdges.listIterator(); 

        for(node<E> adjNode : adjList) 
        {
            adjNode = adjNode.getChild();

            if(!adjNode.isVisited())
            {
                adjNode.setVisited(true);
                visitedNodes.add(adjNode);
            }

        }

    }
    return (null);
}


public void readFile()
{
File file = new File("Enron-Email.txt");

try 
{
    Scanner scanner = new Scanner(file);

    while (scanner.hasNextLine()) 
    {
        String line = scanner.nextLine();
        if(line.trim().startsWith("#"))
        {
            continue; 
        }

        String[] tokens = line.split("\\t");

        Integer parent = Integer.parseInt(tokens[0]);
        Integer child = Integer.parseInt(tokens[1]);

        addEdge((E) parent, (E) child);
    }
    scanner.close();
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}
}
}    


    public class shortestpath{
public static void main(Integer source, Integer dest) {
    graph<E> myGraph = new graph<E>(); 
    myGraph.readFile();
    myGraph.BFS(source);    
}
}

3 个答案:

答案 0 :(得分:1)

您必须在graph.readFile()课程的static上制作readFile()方法instance或致电graph方法。

在第一种情况下,它看起来像是:

public class shortestpath {
    public static void main (String[] args) {
        graph.readFile ();
    }
}

public class graph {
    public static void readFile () {
    }
}

而在第二种情况下,它看起来像是:

public class shortestpath {
    public static void main (String[] args) {
        new graph ().readFile ();
    }
}

public class graph {
    public void readFile () {
    }
}

此外,请注意,尽管在下面的代码中我使用了小写的类名(正如您在问题中提供的那样),但Java世界中有一个通用惯例,即使用upper camel case命名类。

答案 1 :(得分:0)

要从静态方法调用非静态方法,您需要创建实例,在该实例上调用该方法。 因此,在您的情况下,您需要创建一个 Graph类实例,并从 ShortestPath类的主方法中调用readFile()方法。

Graph<YourType> g = new Graph<YourType>(); //replace YourType with the type you want to pass like Integer, blah blah...
g.readFile();

编辑:

graph<E> myGraph = new graph<E>(); 

应该是

graph<Integer> myGraph = new graph<Integer>(); //Or some other valid types. 

E is just an representation of Element. provide a valid element type

答案 2 :(得分:0)

为了调用实例方法,你必须有一个可以调用它的对象。

public class A
{
    public void someMethod()
    {
        ....
    }
}

public class B
{
    public static void main(String[] args)
    {
        A.someMethod(); // ERROR

        A a = new A();
        a.someMethod(); // Correct
    }
}
相关问题