{
class Program
{
static void Main(string[] args)
{
//用多态来模拟实现将移动硬盘或者U盘或者MP3插到电脑上进行读写数据
//MobileDisk md = new MobileDisk();
//UDisk u = new UDisk();
//Mp3 mp3 = new Mp3();
MobileStorage ms = new Mp3();//创建父类对象,用子类赋值
Computer cpu = new Computer();//创建电脑类对象
cpu.Ms = ms;
cpu.CpuRead();
cpu.CpuWrite();
Console.ReadKey();
}
}
/// <summary>
/// 抽象的父类--->可移动存储设备
/// </summary>
public abstract class MobileStorage
{
public abstract void Read();//抽象的方法
public abstract void Write();
}
/// <summary>
/// 子类--->移动硬盘
/// </summary>
public class MobileDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("移动硬盘在读取数据");//重写方法
}
public override void Write()
{
Console.WriteLine("移动硬盘在写入数据");//重写方法
}
}
/// <summary>
/// 子类--->U盘
/// </summary>
public class UDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("U盘在读取数据");
}
public override void Write()
{
Console.WriteLine("U盘在写入数据");
}
}
/// <summary>
/// 子类--->MP3
/// </summary>
public class Mp3 : MobileStorage
{
public override void Read()
{
Console.WriteLine("Mp3读取数据");
}
public override void Write()
{
Console.WriteLine("Mp3写入数据");
}
public void PlayMusic()
{
Console.WriteLine("MP3播放音乐");
}
}
/// <summary>
/// 电脑类
/// </summary>
public class Computer
{
MobileStorage _ms;//定义一个可移动存储设备父类的对象--->字段
public MobileStorage Ms//属性
{
get { return _ms; }
set { _ms = value; }
}
public void CpuRead()
{
Ms.Read();
}
public void CpuWrite()
{
Ms.Write();
}
}
}