简体   繁体   中英

How to specify and use a real interface?

is it possible to specify real interfaces which must implement methods with interfaces?` Or how does this work what i want:

public interface IGraph
{
    void addEdge(IGraphVertex a, IGraphVertex b);
}

public interface IGraphVertex
{

}

// problematic code


public class Graph2d : IGraph
{
    public void addEdge(Graph2dVertex a, Graph2dVertex b)
    {

    }
}

public class Graph2dVertex : IGraphVertex
{

}

the error i get is that Graph2d doesn't implement addVertex(IGraphVertex a, IGraphVertex b);

You want a generic interface:

public interface IGraphVertex
{

}

public interface IGraph<T>
    where T: IGraphVertex
{
    void addEdge(T a, T b);
}

Then:

public class Graph2dVertex : IGraphVertex
{

}

public class Graph2d : IGraph<Graph2dVertex>
{
    public void addEdge(Graph2dVertex a, Graph2dVertex b)
    {

    }
}

You need to fix the method signature in Graph2h class:

public class Graph2d : IGraph
{
   public void addVertex(IGraph2dVertex a, IGraph2dVertex b)
   {

   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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