多态实现之--虚方法

多态实现共有三种途径,分别是:虚方法、抽象类、接口,本篇讲述第一种方法:虚方法。

1. 什么是虚方法?虚方法,从语法上来说是被virtual关键字修饰的方法,从含义上讲,这个方法在类被继承后可以在子类重写(override)。

2. 在子类中重写虚方法需要注意哪些问题?首先说明的是,子类中的重写并不是必须的,因为父类中的虚方法本身也是有函数体的(除非是抽象方法),所以如果进行重写则会覆盖从父类中的虚方法,否则直接调用父类的该方法。

3. 是怎样体现多态的?在调用的时候子类只需要调用同一个方法,如果进行重写则会表现出不同的效果,这就体现出了“多态”,即是同一种写法不同的表现形态,“于同中求不同”。

4.代码示例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _02用虚方法实现多态
 7 {
 8     public class Person
 9     {
10         public Person(string name)
11         {
12             this.Name = name;
13         }
14         public string Name
15         {
16             set;
17             get;
18         }
19         //虚方法,可在子类中重写
20         public virtual void Show()
21         {
22             Console.WriteLine("我是Person");
23         }
24     }
25     /// <summary>
26     /// 中国人类
27     /// </summary>
28     public class Chinese : Person
29     {
30         public Chinese(string name)
31             : base(name)
32         {
33 
34         }
35         //重写Show()方法
36         //可用sealed关键字修饰Show(),查看BeiJingren类中不可再重写Show()方法
37         public override void Show()
38         {
39             Console.WriteLine("我叫:{0},我是中国人", Name);
40         }
41         public void SayHello()
42         {
43             Console.WriteLine("Hello,我是中国人!");
44         }
45     }
46     /// <summary>
47     /// 日本人类
48     /// </summary>
49     public class Janpanese : Person
50     {
51         public Janpanese(string name)
52             : base(name)
53         {
54 
55         }
56         public override void Show()
57         {
58             Console.WriteLine("我叫:{0},我是日本人。", Name);
59         }
60     }
61     /// <summary>
62     /// 美国人类
63     /// </summary>
64     public class American : Person
65     {
66         public American(string name)
67             : base(name)
68         {
69 
70         }
71         //不进行重写的话,在使用American对象调用Show()方法时会直接调用父类Person中的方法。
72         //public override void Show()
73         //{
74         //    Console.WriteLine("我叫:{0},我是美国人。", Name);
75         //}
76     }
77     public class BeiJingren : Chinese
78     {
79         public BeiJingren(string name)
80             : base(name)
81         {
82         }
83         public override void Show()
84         {
85             Console.WriteLine("");
86         }
87     }
88 }
类定义
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _02用虚方法实现多态
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Chinese cn1 = new Chinese("李白");
13             Janpanese jp1 = new Janpanese("松下幸之助");
14             American an1 = new American("Obama");
15 
16             Person[] pn = new Person[] { cn1, jp1, an1 };//建立父类型的数组
17             //遍历整个数组,并使用is判断是否属于某种类型
18             for (int i = 0; i < pn.Length; i++)
19             {
20                 //这句话就体现了多态
21                 pn[i].Show();
22             }
23 
24             //事实证明,多态只适用于子类共有的某些属性或方法,不适用于个性东西。
25             //Person p1 = new Chinese("sk");
26             //p1.SayHello();
27             Console.WriteLine("OK");
28             Console.ReadKey();
29 
30         }
31     }
32 }
主函数
原文地址:https://www.cnblogs.com/gisk/p/4999057.html