抽象类的使用

抽象类:
1、如果一个类中有抽象方法,那么这个类必须是抽象类
2、抽象类中可以有抽象方法,也可以没有抽象方法
3、抽象类不能被实例化
4、抽象类不能是密封类或静态类

子类(普通子类)必须重写父类中的所有抽象方法,
如果子类是抽象类可以不用重写父类的抽象方法。

//抽象类

abstract class Animal
    {
        public abstract void Sound();
    }

class Cat:Animal
    {
        public override void Sound()
        {
            Console.WriteLine("喵喵喵");
        }
    }

class Dog:Animal
    {
        public override void Sound()
        {
            Console.WriteLine("汪汪汪");
        }
    }

class Duck:Animal
    {
        public override void Sound()
        {
            Console.WriteLine("嘎嘎嘎");
        }
    }

//子类是抽象类,可以不重写父类的抽象方法
    abstract class AbstractClass:Animal
    {
    }

class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Dog();
            animal.Sound();
            animal = new Cat();
            animal.Sound();
            animal = new Duck();
            animal.Sound();
        }
    }

原文地址:https://www.cnblogs.com/danmao/p/3871795.html