欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#抽象类与抽象方法详解

来源:本站原创|时间:2020-01-10|栏目: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#使用SQL Dataset数据集代码实例

栏    目:C#教程

下一篇:C#使用SQL DataReader访问数据的优点和实例

本文标题:C#抽象类与抽象方法详解

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5038.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有