using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace Ch12Ex04
{
public class Farm<T> : IEnumerable<T>
where T : Animal
{
private List<T> animals = new List<T>(); public List<T> Animals
{
get
{
return animals;
}
}
//实现IEnumerable接口
public IEnumerable<T> GetEnumrator()
{
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();
}
}
//得到所有Cow或者继承于其的类
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;
}
}
}
这是<<C#入门经典>>里的一例题中的一个泛型类Farm.cs,用已有类Animal来约束。
为何我在编译运行时出现了错误“Ch12Ex04.Farm<T>”不会实现接口成员“System.Collections.Generic.IEnumerable<T>.GetEnumerator()” E:\Chapter12\Ch12Ex04\Ch12Ex04\Farm.cs??请各位帮忙解答下,谢谢
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace Ch12Ex04
{
public class Farm<T> : IEnumerable<T>
where T : Animal
{
private List<T> animals = new List<T>(); public List<T> Animals
{
get
{
return animals;
}
}
//实现IEnumerable接口
public IEnumerable<T> GetEnumrator()
{
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();
}
}
//得到所有Cow或者继承于其的类
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;
}
}
}
这是<<C#入门经典>>里的一例题中的一个泛型类Farm.cs,用已有类Animal来约束。
为何我在编译运行时出现了错误“Ch12Ex04.Farm<T>”不会实现接口成员“System.Collections.Generic.IEnumerable<T>.GetEnumerator()” E:\Chapter12\Ch12Ex04\Ch12Ex04\Farm.cs??请各位帮忙解答下,谢谢
=========================================
public IEnumerator<T> GetEnumerator()
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace Ch12Ex04
{
public abstract class Animal
{
protected string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
//构造函数2
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();
}
}
using System;
using System.Collections.Generic;
using System.Text;namespace Ch12Ex04
{
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);
} }
}
using System;
using System.Collections.Generic;
using System.Text;namespace Ch12Ex04
{
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
}
public Chicken(string newName):base(newName)
{
}
public override void MakeANoise()
{
Console.WriteLine("{0} says '咯咯'", name);
} }
}