When the implementation for the same member signature varies between two or more classes, you have a key object-oriented principal:
polymorphism. Poly" meaning many and "morph" meaning form,
polymorphism
refers to the fact that there are multiple implementations of the same signature. And since the same signature cannot be used multiple times within a single class, each implementation of the member signature occurs on a different class.
The idea behind polymorphism is that the object itself knows best how to perform a particular operation. Given multiple types of documents, each document type class knows best how to perform a Print() method for its corresponding document type. Therefore, instead of defining a single print method that includes a switch statement with the special logic to print each document type, with polymorphism you call the Print() method corresponding to the specific type of document you wish to print. For example, calling Print() on a word processing document class behaves according to word processing specifics, and calling the same method on a graphics document class will result in print behavior specific to the graphic. Given the document types, however, all you have to do to print a document is to call Print(), regardless of the type.
Moving the custom print implementation out of a switch statement offers several maintenance advantages. First, the implementation appears in the context of each document type's class rather than in a location far removed; this is in keeping with encapsulation. Second, adding a new document type doesn't require a change to the switch statement. Instead, all that is necessary is for the new document type class to implement the Print() signature.
OK.Now ,let me have some test.
Code
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 PdaItem[] pda = new PdaItem[3];
6
7 Contact contact = new Contact("Sherlock Holmes");
8 contact.Address = "211B Baker Street,London,England";
9 pda[0] = contact;
10
11 Appointment appointment = new Appointment("Soccer tournament");
12 appointment.StratDataTime = new DateTime(2006, 7, 18);
13 appointment.EndDataTime = new DateTime(2006, 7, 29);
14 appointment.Location = "GuangZhou";
15 pda[1] = appointment;
16
17 contact = new Contact("Anne Frank");
18 contact.Address = "263 Prinsengracht, Amsterdam, Netherlands";
19 pda[2] = contact;
20
21 List(pda);
22 Console.ReadLine();
23
24 }
25
26 // 运用多态执行,基类会识别派生类的执行
27 public static void List(PdaItem[] items)
28 {
29 foreach (PdaItem item in items)
30 {
31 Console.WriteLine("____________");
32 Console.WriteLine(item.GetSummary());
33 }
34 }
35 }
36
37 public abstract class PdaItem
38 {
39 private string _Name;
40
41 // 构造器
42 public PdaItem(string name)
43 {
44 _Name = name;
45 }
46
47 // 抽象属性,可以被重写
48 public virtual string Name
49 {
50 get { return _Name; }
51 set { _Name = value; }
52 }
53 // 定义抽象方法
54 public abstract string GetSummary();
55 }
56
57 public class Contact : PdaItem
58 {
59
60 public Contact(string name) : base(name) { }
61
62
63 // 重写Name属性
64 public override string Name
65 {
66 get
67 {
68 return FirstName + LastName;
69 }
70 set
71 {
72 string[] names = value.Split(' ');
73 FirstName = names[0];
74 LastName = names[1];
75 }
76 }
77
78 private string _FirstName;
79
80 public string FirstName
81 {
82 get { return _FirstName; }
83 set { _FirstName = value; }
84 }
85
86 private string _LastName;
87
88 public string LastName
89 {
90 get { return _LastName; }
91 set { _LastName = value; }
92 }
93
94 private string _Address;
95
96 public string Address
97 {
98 get { return _Address; }
99 set { _Address = value; }
100 }
101
102 public override string GetSummary()
103 {
104 return string.Format("FirstName:{0}\n" + "LastName:{1}\n" + "Address:{2}", FirstName, LastName, Address);
105 }
106 }
107
108 public class Appointment:PdaItem
109 {
110 public Appointment(string name) : base(name) { }
111
112 private DateTime _StratDataTime;
113
114 public DateTime StratDataTime
115 {
116 get { return _StratDataTime; }
117 set { _StratDataTime = value; }
118 }
119
120 private DateTime _EndDataTime;
121
122 public DateTime EndDataTime
123 {
124 get { return _EndDataTime; }
125 set { _EndDataTime = value; }
126 }
127
128 private string _Location;
129
130 public string Location
131 {
132 get { return _Location; }
133 set { _Location = value; }
134 }
135
136 public override string GetSummary()
137 {
138 return string.Format("Location:{0}\n" + "StartDateTime:{1}\n" + "EndDateTime:{2}", Location , StratDataTime , EndDataTime );
139 }
140 }
Result you will print:
With Polymorphism , you can call the method on the base class but the implementation is specific to the derived class.