C# 语言特性系列(2) 多态性 Polymorphism
多态:
1 public class Women
2 {
3 public void DoWork() { }
4 }
5
6 public class Girl : Women
7 {
8 public new void DoWork() { }
9 }
10
11 class Main
12 {
13 static void Main()
14 {
15 Girl B = new Girl();
16 B.DoWork(); // 调Girl方法.
17
18 Women A = (Women)B;
19 A.DoWork(); // 调Women方法.
20 }
21
22 }
2 {
3 public void DoWork() { }
4 }
5
6 public class Girl : Women
7 {
8 public new void DoWork() { }
9 }
10
11 class Main
12 {
13 static void Main()
14 {
15 Girl B = new Girl();
16 B.DoWork(); // 调Girl方法.
17
18 Women A = (Women)B;
19 A.DoWork(); // 调Women方法.
20 }
21
22 }