繁体   English   中英

如何指定和使用真实的界面?

[英]How to specify and use a real interface?

是否有可能指定必须使用接口实现方法的真实接口?`或者这是如何工作的我想要的:

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
{

}

我得到的错误是Graph2d没有实现addVertex(IGraphVertex a, IGraphVertex b);

你想要一个通用的接口:

public interface IGraphVertex
{

}

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

然后:

public class Graph2dVertex : IGraphVertex
{

}

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

    }
}

您需要修复Graph2h类中的方法签名:

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

   }
}

暂无
暂无

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

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