多态实现让电脑去读写各种移动设备的数据
using System; namespace 电脑_移动硬盘_U盘_MP3 { class Program { static void Main(string[] args) { MobileStorage ms = new Udisk(); ms.Read(); ms.Write(); Console.ReadKey(); } //移动存储设备作为顶级父类 public abstract class MobileStorage { public abstract void Read(); public abstract void Write(); } public class MobileDisk:MobileStorage { public override void Read() { Console.WriteLine("移动硬盘在读写数据"); } public override void Write() { Console.WriteLine("移动硬盘在写入数据"); } } public class Udisk:MobileStorage { public override void Read() { Console.WriteLine("U盘在读数据"); } public override void Write() { Console.WriteLine("U盘在写数据"); } } public class Mp3 : MobileStorage { public override void Read() { Console.WriteLine("Mp3读取数据"); } public override void Write() { Console.WriteLine("Mp3写入数据"); } public void PlayMusic() { Console.WriteLine("Mp3自己可以播放音乐"); } } public class Computer { public void CpuRead(MobileStorage ms)//传参 { ms.Read(); } public void CpuWrite(MobileStorage ms) { ms.Write(); } } } }
标签:Console,ms,MobileStorage,void,电脑,多态,public,WriteLine,读写 From: https://www.cnblogs.com/chungeblog/p/17111897.html