.NET基础之自定义泛型分析(2)

#region Animal 虚基类 有一个name属性 Feed方法和一个虚方法MakeANoise
//虚基类 有一个name属性 Feed方法和一个虚方法MakeANoise
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();
}
#endregion

//Cow Animal的子类,实现虚方法
public class Cow:Animal
{
        public Cow(string name) :
            base(name)
        {
        }
        public override void MakeANoise()
        {
            Console.WriteLine("{0} says 'moo!'", name);
        }
}

//Chicken类,Animal子类
public class Chicken:Animal
{
        public Chicken(string name)
            : base(name)
        { }
        public override void MakeANoise()
        {
            Console.WriteLine("{0} says 'cluck'", name);
        }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzgzy.html