码迷,mamicode.com
首页 > 其他好文 > 详细

第二单元练习 第一题和第十一题

时间:2017-09-14 14:50:34      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:his   else   intro   nbsp   console   value   this   bsp   rri   

第一题

animal类

class Animal
    {
        private bool m_sex;
        private int m_age;
        public bool Sex
        {
            get { return m_sex; }
            set { m_sex = value; }
        }
        public int Age
        {
            get { return m_age; }
            set { m_age = value; }
        }
        public Animal()
        {
            Sex = false;
        }
        public virtual void Introduce()
        {
            if (Sex == true)
                Console.WriteLine("This is a male Animal!");
            else
                Console.WriteLine("This is a female Animal!");
        }
    }
    class Dog : Animal
    {
        public Dog()
        {
            Sex = true;
        }
        public override void Introduce()
        {
            if (Sex == true)
                Console.WriteLine("This is a male Dog!");
            else
                Console.WriteLine("This is a female Dog!");
        }
    }
    class Cat : Animal
    {
        public override void Introduce()
        {
            if (Sex == true)
                Console.WriteLine("This is a male Cat!");
            else
                Console.WriteLine("This is a female Cat!");
        }
    }

主函数

 Animal ani = new Animal();
            ani.Introduce();
            Dog dog = new Dog();
            dog.Introduce();
            Cat cat = new Cat();
            cat.Introduce();
            Console.WriteLine("按回车键结束");
            Console.ReadKey();

第十一题

animal类

   class Animal
    {
        private bool m_sex;
        private string m_sound;
        public bool Sex
        {
            get { return m_sex; }
            set { m_sex = value; }
        }
        public string Sound
        {
            get { return m_sound; }
            set { m_sound = value; }
        }
        public Animal()
        {
            m_sex = false;
            m_sound = "Howl";
        }
        public virtual string Roar()
        {
            return Sound;
        }
    }
    class Dog : Animal
    {
        public Dog()
        {
            Sex = true;
            Sound = "Wow...";
        }
        public override  string Roar()
        {
            return "Dog:" + Sound;
        }
    }
    class Cat : Animal
    {
        public Cat()
        {
            Sound = "Miaow...";
        }
        public override string Roar()
        {
            return "Cat:" + Sound;
        }
    }
    class Cow : Animal
    {
        public Cow()
        {
            Sound = "Moo...";
        }
        public override string Roar()
        {
            return "Cow:" + Sound;
        }
    }

主函数

Animal animal;
            Dog dog = new Dog();
            animal = dog;
            Console.WriteLine(animal.Roar());
            Cat cat = new Cat();
            animal = cat;
            Console.WriteLine(animal.Roar());
            Cow cow = new Cow();
            animal = cow;
            Console.WriteLine(animal.Roar());
            Console.WriteLine("按回车键结束");
            Console.ReadKey();

 

第二单元练习 第一题和第十一题

标签:his   else   intro   nbsp   console   value   this   bsp   rri   

原文地址:http://www.cnblogs.com/zjxxy/p/7520244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!