虚方法

 1 using System; 
 2 using System.Collections.Generic;
 3  using System.Linq; using System.Text; 
 4 
 5 namespace ConsoleApplication1 {
 6 
 7  class Program { 
 8 
 9 static void Main(string[] args) {
10 
11  List list = new List(); 
12 list.Add(new Customer()); 
13 list.Add(new VIPACustomer()); 
14 list.Add(new VIPBCustomer()); 
15 list.Add(new VIPCustomer()); 
16 foreach(Customer c in list) { c.Buy(); 
17 
18 }
19  Console.ReadLine(); } 
20 } 
21 
22 class Customer {
23 
24  public virtual void Buy() { 
25 //普通客户使用该方法 
26 Console.WriteLine("Cutomer"); } 
27 
28 } 
29 class VIPCustomer : Customer {
30 
31  public override void Buy() { 
32 
33 Console.WriteLine("VIP Cutomer"); }
34 
35  }
36 
37 
38  class VIPACustomer : VIPCustomer {
39 
40  public override void Buy() {
41 
42  Console.WriteLine("VIP A Cutomer");
43 
44  } 
45 
46 } 
47 
48 class VIPBCustomer : VIPCustomer { 
49 
50 public override void Buy() { 
51 
52 Console.WriteLine("VIP B Cutomer"); } 
53 
54 } 
55 
56 }
View Code
原文地址:https://www.cnblogs.com/linbin524/p/3554790.html