简体   繁体   English

有向图的通用实现

[英]Generic Implementation of Directed Graph

I am having issues in implementing the Generic DirectGraph Implementation.我在实现通用 DirectGraph 实现时遇到问题。 Please help me!请帮我!

public interface DirectedGraph<Vertex<T extends Comparable<T>>> {
   <T extends Comparable<T>> boolean  addVertex(Vertex<T> v);
   <T extends Comparable<T>> boolean addEdge(Vertex<T> v1, Vertex<T> v2);
   int size();
   <T extends Comparable<T>>boolean removeEdge(Vertex<T> v1, Vertex<T> v2);
   <T extends Comparable<T>>boolean hasEdge(Vertex<T> v1, Vertex<T> v2);

}

public class Vertex<E extends Comparable<E>> {
  E vertex;
}

What is wrong with the above code?.上面的代码有什么问题?。 Eclipse is showing the error Eclipse 显示错误

Multiple markers at this line
- Syntax error on token "<", , expected
- The type parameter Vertex is hiding the type 
 Vertex<E>
- Syntax error on token ">>>", >> expected

Either you make a class generic or a method generic.In your case,you are doing both.Also,since you have decided that DirectedGraph will use Vertex type,it is not the class Vertex but type "Vertex" .You use a concrete class or interface type when you use the class or generic method and not in the definiton itself要么你创建一个类泛型或一个泛型方法。在你的情况下,你正在做两者。另外,因为你已经决定DirectedGraph将使用Vertex类型,它不是类 Vertex 而是类型“Vertex” 。你使用一个具体的类或接口类型,当您使用类或泛型方法而不是在定义本身中时

public interface DirectedGraph<T extends Comparable<T>> {
        boolean  addVertex(Vertex<T> v);
        boolean addEdge(Vertex<T> v1, Vertex<T> v2);
        int size();
        boolean removeEdge(Vertex<T> v1, Vertex<T> v2);
        boolean hasEdge(Vertex<T> v1, Vertex<T> v2);

    }

    class Vertex<E extends Comparable<E>> {
      E vertex;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM