C#抽象方法实例

首次发布:2017-06-04 21:10
using System;
using System.Collections.Generic;
using System.Text;

namespace 抽象方法练习
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer dev = new Computer();
            MP3 mp3 = new MP3();
            YDDisk yd = new YDDisk();
            dev.dev = yd;
            dev.dev.Read();//输入是移动硬盘在读数据。。。。。
            dev.dev.Write();
            Console.ReadKey();
        }
    }
    abstract class Disk
    {
        public abstract void Read();
        public abstract void Write();
    }


    class YDDisk : Disk
    {
        public override void Read()
        {
            Console.WriteLine("移动硬盘在读数据。。。。。");
        }
        public override void Write()
        {
            Console.WriteLine("移动硬盘在写数据。。。。。");
        }
    }


    class MP3 : Disk
    {
        public override void Read()
        {
            Console.WriteLine("MP3在读数据。。。。。");
        }
        public override void Write()
        {
            Console.WriteLine("MP3在写数据。。。。。");
        }
        public  void PlayMusic()
        {
            Console.WriteLine("MP3在放音乐。。。。。");
        }
    }

    class Computer
    {
        public Disk dev
        {
            get;
            set;
        }
        void Read()
        {
            dev.Read();
        }
        void Write()
        {
            dev.Write();
        }
    }
}