虚方法

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 }
笔者原创!如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,转载请添加原博客连接,否则保留追究法律责任的权利,谢谢!
YC.Boilerplate 快速开发框架交流,请加群:1060819005
区块链交流请加QQ群:538327407(已满),群2:135019400.
我的博客地址:http://www.cnblogs.com/linbin524/