首页 > 代码库 > 抽象类与抽象方法

抽象类与抽象方法

抽象类为基类:

 

abstract class  Animal     {         string name;

        public string Name         {             get { return name; }             set { name = value; }         }         char gender;

        public char Gender         {             get { return gender; }             set { gender = value; }         }         int age;

        public int Age         {             get { return age; }             set { age = value; }         }         public Animal(string name, char gender, int age)         {             this.name = name;             this.gender = gender;             this.age = age;         }         public   abstract  void Shout();

    

派生类(子类):

  class Cat:Animal
    {
        public Cat(string name, char gender, int age)
            : base(name, gender, age)
        {        
        }
        public   override void Shout()
        {
           Console . WriteLine("喵喵叫!");
        }

  class Dog:Animal
    {
        public Dog(string name, char gender, int age)
            : base(name, gender, age)
        {
        }
        public    override  void Shout()
        {
            Console.WriteLine("汪汪叫!");
        }
    }

主方法:

 class Program     {         static void Main(string[] args)         {             Cat cat=new Cat ("加菲猫",‘公‘,3);             cat.Shout();             Dog dog = new Dog("旺财", ‘母‘, 5);             dog.Shout();             Console.ReadKey();

        }     }