using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MobileStorage mp = new Mp3();
Computer cpu = new Computer();
cpu.Ms = mp;
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()//override可以重写抽象方法和虚方法
{
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
{
private MobileStorage _ms;
public MobileStorage Ms//字段封装属性
{
get { return _ms; }
set { _ms = value; }
}
public void CpuRead()//方法1,想拿到父类,传参数进来就行,可以传子类进来
{
Ms.Read();
}
public void CpuWrite()
{
Ms.Write();
}
}
}