从两个分层接口继承?

时间:2016-04-21 01:59:53

标签: c#

我有两个接口,其中一个具有泛型类型。但是当我尝试使用它们时我不能。

第一个界面是:

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
{
   //......
}

在最后一段代码中,它给出了一个错误。如何编写此层次结构顺序?还有其他选择吗?

1 个答案:

答案 0 :(得分:3)

这是因为您没有在泛型类型定义中的类定义中定义X.我认为你所追求的是这个。

class GAMachine<T> where T : IChromosome<IGene>
{
   //....
}

或者这个

class GAMachine<T, X> where T : IChromosome<X> where X : IGene
{
   //....
}