zoukankan      html  css  js  c++  java
  • 抽象类的使用

    抽象类:
    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();
            }
        }

  • 相关阅读:
    20161101学习笔记
    20161031学习笔记
    20161028学习笔记
    20161027学习笔记
    ReentrantLock Condition
    ReentrantLock 重入锁
    CountDownLatch用法与原理
    场景化解释 AQS原理
    Atomic
    多线程工具类
  • 原文地址:https://www.cnblogs.com/danmao/p/3871795.html
Copyright © 2011-2022 走看看