我想将从具有泛型的一个类派生的不同类型的对象添加到基本类型列表中。我收到此编译错误
Error 2 Argument 1: cannot convert from 'ConsoleApplication1.Stable' to 'ConsoleApplication1.ShelterBase<ConsoleApplication1.AnimalBase>' C:\Users\ysn\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs 43 26 ConsoleApplication1
我看不到问题,您能为我提供一种替代的方式来做这种事情吗?
abstract class AnimalBase { public int SomeCommonProperty;}
abstract class ShelterBase<T> where T : AnimalBase
{
public abstract List<T> GetAnimals();
public abstract void FeedAnimals(List<T> animals);
}
class Horse : AnimalBase { }
class Stable : ShelterBase<Horse>
{
public override List<Horse> GetAnimals()
{
return new List<Horse>();
}
public override void FeedAnimals(List<Horse> animals)
{
// feed them
}
}
class Duck : AnimalBase { }
class HenHouse : ShelterBase<Duck>
{
public override List<Duck> GetAnimals()
{
return new List<Duck>();
}
public override void FeedAnimals(List<Duck> animals)
{
// feed them
}
}
class Program
{
static void Main(string[] args)
{
List<ShelterBase<AnimalBase>> shelters = new List<ShelterBase<AnimalBase>>();
///////////////////////////// following two lines do not compile
shelters.Add(new Stable());
shelters.Add(new HenHouse());
/////////////////////////////
foreach (var shelter in shelters)
{
var animals = shelter.GetAnimals();
// do sth with 'animals' collection
}
}
}
最佳答案
您可以使用contravariance,但前提是您将抽象类更改为接口(interface),并且将GetAnimals
的返回类型更改为IEnumerable<T>
,因为List<T>
不支持此功能。
起作用的代码:
abstract class AnimalBase { public int SomeCommonProperty;}
interface IShelterBase<out T> where T : AnimalBase
{
IEnumerable<T> GetAnimals();
}
class Horse : AnimalBase { }
class Stable : IShelterBase<Horse>
{
public IEnumerable<Horse> GetAnimals()
{
return new List<Horse>();
}
}
class Duck : AnimalBase { }
class HenHouse : IShelterBase<Duck>
{
public IEnumerable<Duck> GetAnimals()
{
return new List<Duck>();
}
}
void Main()
{
List<IShelterBase<AnimalBase>> shelters = new List<IShelterBase<AnimalBase>>();
shelters.Add(new Stable());
shelters.Add(new HenHouse());
foreach (var shelter in shelters)
{
var animals = shelter.GetAnimals();
// do something with 'animals' collection
}
}
关于C#泛型继承问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5909209/