首页 > 其他 > 详细

虚方法

时间:2014-02-19 15:12:55      阅读:281      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
bubuko.com,布布扣
 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
bubuko.com,布布扣

虚方法

原文:http://www.cnblogs.com/linbin524/p/3554790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!