第三个案例(移动设备)

时间:2022-07-12 15:24:03

编写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();
}
}
}