2.1

Animal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    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 string introduce()
        {
            if (Sex)
                return "This is a male Animal!";
            else
                return "This is a female Animal!";     
        }
    }
    class Dog:Animal               //创建派生类并继承Animal
    {
        public Dog()
        {
            Sex = true;
        }
        public new string introduce()        //重载introduce函数
        {
            if (Sex)
                return "This is a male Dog!";
            else
                return "This is a female Dog!";
        }
    }
    class Cat : Animal                           //创建派生类并继承Animal
    {
        public Cat()
        {
            Sex = true;
        }
        public new string introduce()           //重载introduce函数
        {
            if (Sex)
                return "This is a male Cat!";
            else
                return "This is a female Cat!";
        }
    }

}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Animal ani = new Animal();
            Console.WriteLine(ani.introduce());
            Dog dog = new Dog();
            Console.WriteLine(ani.introduce());
            Cat cat = new Cat();
            Console.WriteLine(ani.introduce());
            Console.WriteLine("按回车键结束");
            Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/xx--/p/7594852.html