C# -- 抽象类与抽象方法

C#: 抽象类与抽象方法

1.代码

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             for (int i = 1; i < 10; i++)
 6             {
 7                 if (i % 3 == 1)
 8                 {
 9                     StorageDevice storge1 = new Upan("sandisk--" + i.ToString());
10                     storge1.Inputdevice();
11                     storge1.WriteDataToDevice();
12                 }
13                 else if (i % 3 == 2)
14                 {
15                     StorageDevice storge2 = new YingPan("westdata--" + i.ToString());
16                     storge2.Inputdevice();
17                     storge2.WriteDataToDevice();
18                 }
19                 else if (i % 3 == 0)
20                 {
21                     StorageDevice storge3 = new MobliePhone("iphone--" + i.ToString());
22                     storge3.Inputdevice();
23                     storge3.WriteDataToDevice();
24                 }
25             }
26             Console.ReadKey();
27         }
28     }
29 
30     abstract class StorageDevice
31     {
32         public abstract void Inputdevice();
33         public abstract void WriteDataToDevice();
34     }
35 
36     class Upan : StorageDevice
37     {
38         public Upan(string name)
39         {
40             this.Name = name;
41         }
42 
43         private string Name;
44         public override void Inputdevice()
45         {
46             Console.WriteLine("U pan ({0}) input the computer.....", Name);
47         }
48 
49         public override void WriteDataToDevice()
50         {
51             Console.WriteLine("U pan ({0}) write data.....", Name);
52         }
53     }
54 
55     class MobliePhone : StorageDevice
56     {
57         private string Name;
58         public MobliePhone(string name)
59         {
60             this.Name = name;
61         }
62         public override void Inputdevice()
63         {
64             Console.WriteLine("Mobile Phone ({0}) input the computer....", Name);
65         }
66         public override void WriteDataToDevice()
67         {
68             Console.WriteLine("Mobile Phone ({0}) write data....", Name);
69         }
70     }
71 
72     class YingPan : StorageDevice
73     {
74         public YingPan(string name)
75         {
76             this.Name = name;
77         }
78         public string Name { get; private set; }
79 
80         public override void Inputdevice()
81         {
82             Console.WriteLine("Ying pan ({0}) input the computer...", Name);
83         }
84         public override void WriteDataToDevice()
85         {
86             Console.WriteLine("Ying pan ({0}) write data...", Name);
87         }
88     }

2. 运行结果:
C# -- 抽象类与抽象方法_第1张图片

转载于:https://www.cnblogs.com/ChengWenHao/p/ChouXiangLei.html

你可能感兴趣的:(C# -- 抽象类与抽象方法)