C#: 抽象类与抽象方法
1.代码
class Program
{
static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
if (i % == )
{
StorageDevice storge1 = new Upan("sandisk--" + i.ToString());
storge1.Inputdevice();
storge1.WriteDataToDevice();
}
else if (i % == )
{
StorageDevice storge2 = new YingPan("westdata--" + i.ToString());
storge2.Inputdevice();
storge2.WriteDataToDevice();
}
else if (i % == )
{
StorageDevice storge3 = new MobliePhone("iphone--" + i.ToString());
storge3.Inputdevice();
storge3.WriteDataToDevice();
}
}
Console.ReadKey();
}
} abstract class StorageDevice
{
public abstract void Inputdevice();
public abstract void WriteDataToDevice();
} class Upan : StorageDevice
{
public Upan(string name)
{
this.Name = name;
} private string Name;
public override void Inputdevice()
{
Console.WriteLine("U pan ({0}) input the computer.....", Name);
} public override void WriteDataToDevice()
{
Console.WriteLine("U pan ({0}) write data.....", Name);
}
} class MobliePhone : StorageDevice
{
private string Name;
public MobliePhone(string name)
{
this.Name = name;
}
public override void Inputdevice()
{
Console.WriteLine("Mobile Phone ({0}) input the computer....", Name);
}
public override void WriteDataToDevice()
{
Console.WriteLine("Mobile Phone ({0}) write data....", Name);
}
} class YingPan : StorageDevice
{
public YingPan(string name)
{
this.Name = name;
}
public string Name { get; private set; } public override void Inputdevice()
{
Console.WriteLine("Ying pan ({0}) input the computer...", Name);
}
public override void WriteDataToDevice()
{
Console.WriteLine("Ying pan ({0}) write data...", Name);
}
}
2. 运行结果: