本文实例为大家分享了C#抽象类与抽象方法的具体实现代码,供大家参考,具体内容如下
1.代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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. 运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/ChengWenHao/p/ChouXiangLei.html