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的类则可以。

  • 相关阅读:
    Codeforces 691A Fashion in Berland
    HDU 5741 Helter Skelter
    HDU 5735 Born Slippy
    HDU 5739 Fantasia
    HDU 5738 Eureka
    HDU 5734 Acperience
    HDU 5742 It's All In The Mind
    POJ Euro Efficiency 1252
    AtCoder Beginner Contest 067 C
    AtCoder Beginner Contest 067 D
  • 原文地址:https://www.cnblogs.com/zzunstu/p/9115455.html
Copyright © 2011-2022 走看看