首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请各位帮小弟我看下这个程序

2011-12-19 
请各位帮我看下这个程序昨晚编译《C#入门经典》一例题时出现了错误“Ch12Ex04.Farm T ”不会实现接口成员“Sy

请各位帮我看下这个程序
昨晚编译《C#入门经典》一例题时出现了错误“Ch12Ex04.Farm <T> ”不会实现接口成员“System.Collections.Generic.IEnumerable <T> .GetEnumerator()”E:\BegVCSharp\Chapter12\Ch12Ex04\Ch12Ex04\Farm.cs
可是我在泛型类Farm中已经实现了接口IEnumerable,不知道到底错在哪里,下面是我的源代码:
Animal.cs
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;
                        }
                }
                  //构造函数
                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();
        }
}
--------------------------------
--------------------------------
Chicken.cs
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);
                }

        }
}
--------------------------------
--------------------------------
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   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   'I 'm   super   cow ",   name);
                }
        }
}
=================================================================
报错的就是这个泛型
Farm.cs
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;
                        }
                }
                //实现接口IEnumrable
                public   IEnumerable <T>   GetEnumrator()
                {
                        return   animals.GetEnumerator();
                }

                IEnumerator   IEnumerable.GetEnumerator()
                {
                        return   animals.GetEnumerator();
                }
                //迭代Animal类的方法
                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;
                }
        }
}
=====================================================================
客户代码
using   System;
using   System.Collections.Generic;
using   System.Text;

namespace   Ch12Ex04
{
        class   Program
        {
                static   void   Main(string[]   args)
                {
                        Farm <Animal>   farm   =   new   Farm <Animal> ();
                        farm.Animals.Add(new   Cow( "Jack "));
                        farm.Animals.Add(new   Chicken( "John "));
                        farm.Animals.Add(new   Chicken( "Joe "));
                        farm.Animals.Add(new   SuperCow( "Sam "));
                        farm.MakeNoises();

                        Farm <Cow>   dairyFarm   =   farm.GetCows();
                        dairyFarm.FeedTheAnimals();

                        foreach   (Cow   cow   in   dairyFarm)
                        {


                                if   (Cow   is   SuperCow)
                                {
                                        (cow   as   SuperCow).Fly();
                                }
                        }
                        Console.ReadKey();
                }
        }
}

[解决办法]
public IEnumerable <T> GetEnumrator()
{
return animals.GetEnumerator();
}

===================>

public IEnumerator <T> GetEnumrator()
{
return animals.GetEnumerator();
}

热点排行