现在有这样几个类//这个是父类public abstract class Animal
{
protected string name;
public string Name
{
get
{
return name;
}
set { name = value; }
}
public Animal()
{
name = "the animal with no name";
}
public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
public abstract void MakeANoise();
}
然后下面是子类//Cow子类public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
}
public Cow(string newName)
: base(newName)
{ }
public override void MakeANoise()
{
Console.WriteLine("{0} says 'moo!'", name);
}
}//SuperCowpublic class SuperCow:Cow
{
public void Fly()
{
Console.WriteLine("{0} is flying!", name);
}
public SuperCow(string newName) : base(newName) { } public override void MakeANoise()
{
Console.WriteLine("{0} says 'here I come to save the day!'", name);
}
}
然后在一个泛型类里面这么写public class Farm<T> : IEnumerable<T>
where T : Animal
{
private List<T> animals = new List<T>(); public List<T> Animals
{
get { return animals; }
}
public IEnumerable<T> GetEnumerator()
{
return animals.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return animals.GetEnumerator();
}
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoise();
} public void FeedTheAnimals()
{
foreach (T animal in animals)
animal.Feed();
}
public Farm<Cow> GetCows()
{
Farm<Cow> cowFarm = new Farm<Cow>();
foreach (T animal in animals)
{
if (animal is Cow)
{
cowFarm.Animals.Add(animal as Cow);
}
}
return cowFarm;
}
}
这都是按照书上写的,但为什么会报这样的错
错误 1 “ConsoleExample.Farm<T>”不会实现接口成员“System.Collections.Generic.IEnumerable<T>.GetEnumerator()”。“ConsoleExample.Farm<T>.GetEnumerator()”或者是静态、非公共的,或者有错误的返回类型。 G:\c#file\ConsoleExample\ConsoleExample\Farm.cs 9 18 ConsoleExample
请问应该怎么解决呢?
{
protected string name;
public string Name
{
get
{
return name;
}
set { name = value; }
}
public Animal()
{
name = "the animal with no name";
}
public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
public abstract void MakeANoise();
}
然后下面是子类//Cow子类public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
}
public Cow(string newName)
: base(newName)
{ }
public override void MakeANoise()
{
Console.WriteLine("{0} says 'moo!'", name);
}
}//SuperCowpublic class SuperCow:Cow
{
public void Fly()
{
Console.WriteLine("{0} is flying!", name);
}
public SuperCow(string newName) : base(newName) { } public override void MakeANoise()
{
Console.WriteLine("{0} says 'here I come to save the day!'", name);
}
}
然后在一个泛型类里面这么写public class Farm<T> : IEnumerable<T>
where T : Animal
{
private List<T> animals = new List<T>(); public List<T> Animals
{
get { return animals; }
}
public IEnumerable<T> GetEnumerator()
{
return animals.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return animals.GetEnumerator();
}
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoise();
} public void FeedTheAnimals()
{
foreach (T animal in animals)
animal.Feed();
}
public Farm<Cow> GetCows()
{
Farm<Cow> cowFarm = new Farm<Cow>();
foreach (T animal in animals)
{
if (animal is Cow)
{
cowFarm.Animals.Add(animal as Cow);
}
}
return cowFarm;
}
}
这都是按照书上写的,但为什么会报这样的错
错误 1 “ConsoleExample.Farm<T>”不会实现接口成员“System.Collections.Generic.IEnumerable<T>.GetEnumerator()”。“ConsoleExample.Farm<T>.GetEnumerator()”或者是静态、非公共的,或者有错误的返回类型。 G:\c#file\ConsoleExample\ConsoleExample\Farm.cs 9 18 ConsoleExample
请问应该怎么解决呢?
解决方案 »
- 求wpf中文教程
- 关于datagridview 的小问题哦
- 求一条VBA语句转换成为C#
- c#三角形输出
- 高分跪求正则表达式
- 初学者问题,datagridview导出到excel,添加引用问题
- 请问思归等高手,我的WEBFORM中DATAGRID中某字段超链接问题
- 如何使CrystalReport 打印运行中自定义产生的datatable数据?
- 在C#中如何实现子窗体,即像VB6中的MdiForm,子窗体嵌入在主窗体中。谢谢!
- 高手帮忙,江湖救急!有关VC程序改成c#程序问题!!!!!!!!!!!!!!
- 如何用C#修改文件夹的权限?
- 求教:C#如何获得word文档中字体颜色的属性(在线等~~急~~)
{
return animals.GetEnumerator();
}
将IEnumerable<T>删除掉,重新写为IEnumerator<T>就行了。