namespace 电脑_移动硬盘_U盘_MP3
{
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();
}
}
///
/// 抽象的父类--->可移动存储设备
///
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("移动硬盘在写入数据");//重写方法
}
}
///
/// 子类--->U盘
///
public class UDisk : MobileStorage
{
public override void Read()
{
Console.WriteLine("U盘在读取数据");
}
public override void Write()
{
Console.WriteLine("U盘在写入数据");
}
}
///
/// 子类--->MP3
///
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
{
MobileStorage _ms;//定义一个可移动存储设备父类的对象--->字段
public MobileStorage Ms//属性
{
get { return _ms; }
set { _ms = value; }
}
public void CpuRead()
{
Ms.Read();
}
public void CpuWrite()
{
Ms.Write();
}
}
}