最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#抽象类与抽象方法详解
时间:2022-06-25 07:40:44 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了C#抽象类与抽象方法的具体实现代码,供大家参考,具体内容如下
1.代码
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 10; i++)
{
if (i % 3 == 1)
{
StorageDevice storge1 = new Upan("sandisk--" + i.ToString());
storge1.Inputdevice();
storge1.WriteDataToDevice();
}
else if (i % 3 == 2)
{
StorageDevice storge2 = new YingPan("westdata--" + i.ToString());
storge2.Inputdevice();
storge2.WriteDataToDevice();
}
else if (i % 3 == 0)
{
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. 运行结果:
相关文章
- 将魂师怎么获得b级神君 b级神君获取及培育方法攻略 12-16
- poki小游戏入口在哪里精准直达-poki小游戏入口在哪里一键速查入口 12-16
- 逆战未来磐石机甲如何使用 12-16
- 漫蛙网页版免费入口分享 manwa漫画免费阅读页面 12-16
- 巨神军师甄姬怎么组队 12-16
- 蛙漫画官方页面免费漫画观看与下载入口-蛙漫画官方正版免费资源 12-16
