欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-09 19:06:21
本文实例为大家分享了c#抽象类与抽象方法的具体实现代码,供大家参考,具体内容如下 1.代码 class program { static void m...

本文实例为大家分享了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#抽象类与抽象方法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。