多态

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //概念:让一个对象能够表现出多种的状态(类型)
 6             //实现多态的3种手段:1、虚方法 2、抽象类 3、接口
 7 
 8             Chinese cn1 = new Chinese("韩梅梅");
 9             Chinese cn2 = new Chinese("李雷");
10             Japanese j1 = new Japanese("树下君");
11             Japanese j2 = new Japanese("井边子");
12             Korea k1 = new Korea("金秀贤");
13             Korea k2 = new Korea("金贤秀");
14             American a1 = new American("科比布莱恩特");
15             American a2 = new American("奥尼尔");
16             Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2, new English("格林"), new English("玛利亚") };
17 
18             for (int i = 0; i < pers.Length; i++)
19             {
20                 //if (pers[i] is Chinese)
21                 //{
22                 //    ((Chinese)pers[i]).SayHello();
23                 //}
24                 //else if (pers[i] is Japanese)
25                 //{
26                 //    ((Japanese)pers[i]).SayHello();
27                 //}
28                 //else if (pers[i] is Korea)
29                 //{
30                 //    ((Korea)pers[i]).SayHello();
31                 //}
32                 //else
33                 //{
34                 //    ((American)pers[i]).SayHello();
35                 //}
36 
37 
38                 pers[i].SayHello();
39             }
40             Console.ReadKey();
41         }
42     }
 1     public class Person
 2     {
 3         private string _name;
 4         public string Name
 5         {
 6             get { return _name; }
 7             set { _name = value; }
 8         }
 9 
10         public Person(string name)
11         {
12             this.Name = name;
13         }
14         public virtual void SayHello()
15         {
16             Console.WriteLine("我是人类");
17         }
18     }
Person
 1     public class Chinese : Person
 2     {
 3         public Chinese(string name)
 4             : base(name)
 5         {
 6 
 7         }
 8 
 9         public override void SayHello()
10         {
11             Console.WriteLine("我是中国人,我叫{0}", this.Name);
12         }
13     }
Chinese : Person
 1     public class Japanese : Person
 2     {
 3         public Japanese(string name)
 4             : base(name)
 5         { }
 6 
 7         public override void SayHello()
 8         {
 9             Console.WriteLine("我是脚盆国人,我叫{0}", this.Name);
10         }
11     }
Japanese : Person
 1     public class Korea : Person
 2     {
 3         public Korea(string name)
 4             : base(name)
 5         {
 6 
 7         }
 8 
 9         public override void SayHello()
10         {
11             Console.WriteLine("我是棒之思密达,我叫{0}", this.Name);
12         }
13     }
Korea : Person
 1     public class American : Person
 2     {
 3         public American(string name)
 4             : base(name)
 5         {
 6 
 7         }
 8 
 9         public override void SayHello()
10         {
11             Console.WriteLine("我叫{0},我是米国人", this.Name);
12         }
13     }
American : Person
 1     public class English : Person
 2     {
 3         public English(string name)
 4             : base(name)
 5         { }
 6 
 7         public override void SayHello()
 8         {
 9             Console.WriteLine("我是英国人");
10         }
11     }
English : Person

 

posted @ 2020-09-22 10:39  技术不够脸来凑  阅读(118)  评论(0)    收藏  举报