用多態來實現隨身碟,Mp3,移動硬碟和電腦的對接,讀取寫入資料。

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 多態類比移動硬碟和隨身碟{    class Program    {        static void Main(string[] args)        {            //用多態來實現隨身碟,Mp3,移動硬碟和電腦的對接,讀取寫入資料。            MobileTool mt = new UDisk();            Computer cpu = new Computer();            cpu.CpuRead(mt);            cpu.CpuWrite(mt);            Console.ReadKey();        }        /// <summary>        /// 抽象的父類        /// </summary>        public abstract class MobileTool        {            public abstract void Read();            public  abstract void Write();        }        public class UDisk : MobileTool        {            public override void Read()            {                Console.WriteLine("隨身碟讀取成功");            }            public override void Write()            {                Console.WriteLine("隨身碟寫入成功");            }        }        public class Mp3Disk : MobileTool        {            public override void Read()            {                Console.WriteLine("Mp3讀取成功");            }            public override  void Write()            {                Console.WriteLine("Mp3寫入成功");            }            public void PlayMuisc()            {                Console.WriteLine("Mp3自己可以播放音樂");            }        }        public class MobileDisk : MobileTool        {            public override void Read()            {                Console.WriteLine("移動硬碟讀取成功");            }            public override void Write()            {                Console.WriteLine("移動硬碟寫入成功");            }        }        public class Computer        {            public void CpuRead(MobileTool mt)            {                mt.Read();             }            public void CpuWrite(MobileTool mt )            {                mt.Read();            }        }    }}

在computer抓取父類的方法有多種:可以方法中傳遞參數抓取,可以通過建構函式,也可以通過屬性來抓取。

 static void Main(string[] args)        {               MobileTool mt = new UDisk();            Computer cpu = new Computer();            cpu.Mt = mt;            cpu.CpuRead();            cpu.CpuWrite();            Console.ReadKey();        }     public class Computer        {            private MobileTool _mt;            internal MobileTool Mt            {                get { return _mt; }                set { _mt = value; }            }            public void CpuRead()            {                Mt.Read();             }            public void CpuWrite()            {                Mt.Read();

 

用多態來實現隨身碟,Mp3,移動硬碟和電腦的對接,讀取寫入資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.