zoukankan      html  css  js  c++  java
  • C#视频

    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

    抽象方法:

    抽象方法本身就是一个隐含的virtual虚方法,也可以实现多态。抽象方法必须声明在抽象类中。抽象方法不能声明为private。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
    
        abstract class Employee
        {
            private string _name;
    
            protected Employee(string name)
            {
                this._name = name;
            }
    
            public string Name
            {
                get
                {
                    return this._name;
                }
            }
    
            public abstract void startWork();
        }
    
        class Manager : Employee
        {
            public Manager(string name)
                : base(name)
            {
    
            }
    
            public override void startWork()
            {
                Console.WriteLine("{0} starts to distrubit tasks", base.Name);
            }
        }
    
        class Secretary : Employee
        {
            public Secretary(string name)
                : base(name)
            {
    
            }
    
            public override void startWork()
            {
                Console.WriteLine("{0} starts to help", base.Name);
            }
        }
    
        class Seller : Employee
        {
            public Seller(string name)
                : base(name)
            {
    
            }
    
            public override void startWork()
            {
                Console.WriteLine("{0} starts to sell", base.Name);
            }
        }
    
        class Accountant : Employee
        {
            public Accountant(string name)
                : base(name)
            {
    
            }
    
            public override void startWork()
            {
                Console.WriteLine("{0} starts to compute", base.Name);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Employee[] es = new Employee[5];
    
                es[0] = new Manager("YY");
                es[1] = new Manager("Gan");
                es[2] = new Secretary("Kate");
                es[3] = new Seller("Jim");
                es[4] = new Accountant("Tata");
    
                foreach (Employee e in es)
                {
                    e.startWork();
                }
    
                Console.ReadKey();
            }
        }
    }
    View Code

    虚方法和抽象方法
    当所有子类都要实现一些共同的功能的时候,应用虚方法。

     包含抽象方法的类不能被实例化,而包含virtual的类则可以。

  • 相关阅读:
    WF4 工作流事件顺序
    审批工作流系统预览
    系统框架最终集大成之——目录
    11.34 为什么框架没有提供代码设计器或代码生成器?
    11.35 如何编写自动任务?
    11.37 如何在系统中记录日志?
    11.38 CastleActiveRecord中如何保证多线程并发操作的安全与成功?
    关于数据库移植方面的记录
    十二、 结语
    datagridview某列编辑时显示为大写
  • 原文地址:https://www.cnblogs.com/zzunstu/p/9115455.html
Copyright © 2011-2022 走看看