编写ModelDisk父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mobile
{
public abstract class ModelDisk
{
public abstract void Read();
public abstract void Write();
}
}
编写UDisk类继承父类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mobile
{
public class UDisk : ModelDisk
{
public override void Read()
{
Console.WriteLine("Read UDISK");
}
public override void Write()
{
Console.WriteLine("Write UDISK");
}
}
}
主函数的编写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mobile
{
class Program
{
static void Main(string[] args)
{
Computer c = new Computer();
c.MDK = new UDisk();
c.Read();
c.Write();
Console.ReadKey();
}
}
}
Computer类的实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mobile
{
public class Computer
{
public ModelDisk MDK { get; set; }
public void Read()
{
MDK.Read();
}
public void Write()
{
MDK.Write();
}
}
}