我有两个接口,其中一个具有泛型类型。但是当我尝试使用它们时我不能。
第一个界面是:
interface IGene
{
string Name { get; set; }
int Index { get; set; }
}
第二个是:
interface IChromosome<X> where X :IGene
{
double Fitness { get; }
X[] Genes { get; set; }
}
实现这些的类:
class GAMachine<T> where T : IChromosome<X> where X:IGene
{
//......
}
在最后一段代码中,它给出了一个错误。如何编写此层次结构顺序?还有其他选择吗?
答案 0 :(得分:3)
这是因为您没有在泛型类型定义中的类定义中定义X.我认为你所追求的是这个。
class GAMachine<T> where T : IChromosome<IGene>
{
//....
}
或者这个
class GAMachine<T, X> where T : IChromosome<X> where X : IGene
{
//....
}