[英]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);
我得到的错误是Graph2d没有实现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: 您需要修复Graph2h类中的方法签名:
public class Graph2d : IGraph
{
public void addVertex(IGraph2dVertex a, IGraph2dVertex b)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.