码迷,mamicode.com
首页 > Windows程序 > 详细

C#视频 - 虚方法, virtual

时间:2018-05-31 11:29:43      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:pen   name   string   write   .com   back   star   style   line   

技术分享图片

技术分享图片

技术分享图片

Note: virtual 和 abstract 方法不能声明为 private。

技术分享图片
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Collections;
  7 
  8 namespace ConsoleApplication1
  9 {
 10 
 11     class Employee
 12     {
 13         private string _name;
 14 
 15         public Employee(string name)
 16         {
 17             this._name = name;
 18         }
 19 
 20         public string Name
 21         {
 22             get
 23             {
 24                 return this._name;
 25             }
 26         }
 27 
 28         public virtual void startWork()
 29         {
 30             Console.WriteLine("{0} starts work.", this._name);
 31         }
 32     }
 33 
 34     class Manager : Employee
 35     {
 36         public Manager(string name): base(name)
 37         {
 38             
 39         }
 40 
 41         public override void startWork()
 42         {
 43             Console.WriteLine("{0} starts to distrubit tasks", base.Name);
 44         }
 45     }
 46 
 47     class Secretary : Employee
 48     {
 49         public Secretary(string name)
 50             : base(name)
 51         {
 52 
 53         }
 54 
 55         public override void startWork()
 56         {
 57             Console.WriteLine("{0} starts to help", base.Name);
 58         }
 59     }
 60 
 61     class Seller : Employee
 62     {
 63         public Seller(string name)
 64             : base(name)
 65         {
 66 
 67         }
 68 
 69         public override void startWork()
 70         {
 71             Console.WriteLine("{0} starts to sell", base.Name);
 72         }
 73     }
 74 
 75     class Accountant : Employee
 76     {
 77         public Accountant(string name)
 78             : base(name)
 79         {
 80 
 81         }
 82 
 83         public override void startWork()
 84         {
 85             Console.WriteLine("{0} starts to compute", base.Name);
 86         }
 87     }
 88 
 89     class Program
 90     {
 91         static void Main(string[] args)
 92         {
 93             Employee[] es = new Employee[5];
 94 
 95             es[0] = new Employee("Tim");
 96             es[1] = new Manager("Gan");
 97             es[2] = new Secretary("Kate");
 98             es[3] = new Seller("Jim");
 99             es[4] = new Accountant("Tata");
100 
101             foreach(Employee e in es)
102             {
103                 e.startWork();
104             }
105 
106             Console.ReadKey();
107         }
108     }
109 }
View Code

 

C#视频 - 虚方法, virtual

标签:pen   name   string   write   .com   back   star   style   line   

原文地址:https://www.cnblogs.com/zzunstu/p/9115455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!