C#中的多态

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _14用虚方法实现多态
 7 {
 8     public class Person
 9     {
10         public Person(string name)
11         {
12             this.Name = name;
13         }
14         public string Name
15         {
16             get;
17             set;
18         }
19 
20         //1.第一,将父类中的Show方法标记为“虚方法”,virtual
21         //2.当一个方法是虚方法的时候,那么这个方法在子类中就可以被重写了.
22         //子类继承父类以后,可以直接使用该方法,也可以将该方法重写。
23         public virtual void Show()
24         {
25             Console.WriteLine("父类中的Show方法。");
26         }
27     }
28 
29     /// <summary>
30     /// 中国人类
31     /// </summary>
32     public class Chinese : Person
33     {
34         public Chinese(string name)
35             : base(name)
36         {
37 
38         }
39 
40         //2.子类如果要重写父类中的方法,则需要在子类的Show方法前,加一个override关键字
41         //子类重写父类的方法必须并且只能用override关键。
42         public override void Show()
43         {
44             Console.WriteLine("我叫:{0},我是中国人!", Name);
45         }
46     }
47 
48     /// <summary>
49     /// 日本人类
50     /// </summary>
51     public class Japanese : Person
52     {
53         public Japanese(string name)
54             : base(name)
55         {
56 
57         }
58         public override void Show()
59         {
60             Console.WriteLine("我叫:{0},我是日本人!", Name);
61         }
62     }
63 
64     public class American : Person
65     {
66         public American(string name)
67             : base(name)
68         {
69 
70         }
71         public override void Show()
72         {
73             Console.WriteLine("我叫:{0},我是美国人。", Name);
74         }
75     }
76 
77     /// <summary>
78     /// 英国人类。
79     /// </summary>
80     public class English : Person
81     {
82         public English(string name)
83             : base(name)
84         {
85 
86         }
87 
88         public override void Show()
89         {
90             Console.WriteLine("我叫:{0},我是英国人。", Name);
91         }
92     }
93 
94 
95 }
person类

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _15通过虚方法实现方法重写的练习
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //Manager man = new Manager();
13             //man.DaKa();
14             //Console.ReadKey();
15 
16             //Employee emp = new ShaoGuoluDe();//new Manager();
17             //emp.DaKa();
18             //Console.ReadKey();
19 
20             Person p = new Driver();
21             p.SayHello();
22             Console.ReadKey();
23         }
24     }
25 
26     class Employee
27     {
28         public virtual void DaKa()
29         {
30             Console.WriteLine("员工早上8:00打卡。");
31         }
32     }
33 
34     class Manager : Employee
35     {
36         public override void DaKa()
37         {
38             Console.WriteLine("经理早上7:30打卡。");
39         }
40     }
41 
42     class ShaoGuoluDe : Employee
43     {
44         public override void DaKa()
45         {
46             Console.WriteLine("晚上12:00打卡。");
47         }
48     }
49 
50 
51 }
多态的实现
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _19sealed关键字_1密封类
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //Chinese c = new Chinese();
13 
14             //Console.WriteLine("ok");
15             //Console.ReadKey();
16             //string s = "aa";
17 
18             Person p = new BeiJingren();
19             p.SayHi();
20             Console.ReadKey();
21 
22         }
23     }
24 
25     public class Person
26     {
27         public string Name
28         {
29             get;
30             set;
31         }
32         public int Age
33         {
34             get;
35             set;
36         }
37         public string Email
38         {
39             get;
40             set;
41         }
42 
43         public virtual void SayHi()
44         {
45             Console.WriteLine("hi in Person.");
46         }
47     }
48 
49     class Chinese : Person
50     {
51         //禁止子类重写该方法。
52         public sealed override void SayHi()
53         {
54             Console.WriteLine("hi in Chinese");
55         }
56     }
57 
58     class BeiJingren : Chinese
59     {
60         //public override void SayHi()
61         //{
62         //    Console.WriteLine("hi in BeiJingren");
63         //}
64     }
65 
66 
67     //public sealed class Person
68     //{
69     //    public string Name
70     //    {
71     //        get;
72     //        set;
73     //    }
74     //    public int Age
75     //    {
76     //        get;
77     //        set;
78     //    }
79     //    public string Email
80     //    {
81     //        get;
82     //        set;
83     //    }
84     //}
85 
86     //class Chinese : Person
87     //{
88 
89     //}
90     //class MyString:String
91     //{
92 
93     //}
94 }
密封类

当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承

 

 

 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 
13             Chinese cn = new Chinese();
14 
15             //抽象类是不能被实例化的。(抽象类的作用就是为了让继承。)
16            // Person p = new Person();
17 
18 
19         }
20     }
21 
22     //这个类也必须标记为abstract(抽象)的。
23     //抽象成员必须写在抽象类中。不能写在一个实例中。
24     public abstract class Person
25     {
26 
27         public string Name
28         {
29             get;
30             set;
31         }
32         //可以包含抽象方法、属性、索引器、事件。【其实这几个最后都是方法。】
33         //抽象属性,没有任何实现(不要与自动属性误解)
34         public abstract int Age
35         {
36             get;
37             set;
38         }
39 
40         public void SayHi()
41         {
42             Console.WriteLine("Hi~~,我不抽象。");
43         }
44 
45         //1.抽象方法不能有任何方法体。(不能有任何实现)
46         //抽象成员不能是private
47         public abstract void Show();
48     }
49 
50     public class Chinese : Person
51     {
52 
53         /// <summary>
54         /// 3.父类中的抽象成员,子类继承以后必须重写(例外:除非子类也是一个抽象类。)
55         /// </summary>
56         public override void Show()
57         {
58             Console.WriteLine("我是中国人!");
59         }
60 
61         public override int Age
62         {
63             get
64             {
65                 throw new NotImplementedException();
66             }
67             set
68             {
69                 throw new NotImplementedException();
70             }
71         }
72     }
73 
74     //抽象类中
75     public abstract class Japanese : Person
76     {
77 
78     }
79 }
sealed

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _09new关键字用来隐藏父类方法
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             ////Person p = new Chinese();
13             ////p.SayHi();
14             ////Console.ReadKey();
15 
16             //Chinese cn = new Chinese();
17             //cn.SayHi();
18             //Console.ReadKey();
19 
20             B b = new B();
21             b.SayHello();
22             Console.ReadKey();
23         }
24     }
25     class Person
26     {
27         public virtual void SayHi()
28         {
29             Console.WriteLine("父类中的SayHI方法!");
30         }
31     }
32 
33     class Chinese : Person
34     {
35         ////方法重写
36         //public override void SayHi()
37         //{
38         //    Console.WriteLine("子类中的SayHi");
39         //}
40 
41         //在子类中写了一个全新的方法SayHi,同时隐藏了从父类继承下来的SayHi方法。
42         public new void SayHi()
43         {
44             Console.WriteLine("子类中的SayHi");
45         }
46     }
47 }
NEW()来重写虚方法

 

 

总结 : 实现多态的方式有三种方法,标记类的方法为virtual虚方法,override重载的方法,abstract抽象类下的抽象方法、new()重写虚方法等及属性。想让重载的方法无法被继承就使用sealed关键字

posted @ 2013-07-17 19:46  墓地de小草  阅读(175)  评论(0编辑  收藏  举报