是否可以指定必须用接口(interface)实现方法的真实接口(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);
最佳答案
你想要一个通用接口(interface):
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)
{
}
}
关于c# - 如何指定和使用真实的接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141004/