这篇文章主要为大家详细介绍了C#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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. 运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:C#抽象类与抽象方法详解
基础教程推荐
猜你喜欢
- C# List实现行转列的通用方案 2022-11-02
- C# windows语音识别与朗读实例 2023-04-27
- unity实现动态排行榜 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- 一个读写csv文件的C#类 2022-11-06