多态初步
人:(理发师)(医生)(演员)
1 public class Doctor:Person 2 { 3 public override void Cut() 4 { 5 Console.WriteLine("医生的Cut"); 6 7 } 8 }//医生类
public class Hairdresser:Person { public override void Cut() { Console.WriteLine("理发师的Cut"); } }//理发师
1 public class Person 2 { 3 public virtual void Cut() 4 { 5 Console.WriteLine("父类的cut方法"); 6 } 7 }
1 public class Actor:Person 2 { 3 public override void Cut() 4 { 5 Console.WriteLine("演员的Cut"); 6 } 7 }
1 static void Main(string[] args) 2 { 3 4 List<Person> list =new List<Person>() 5 { 6 new Hairdresser(), 7 new Doctor(), 8 new Actor() 9 }; 10 11 12 foreach (Person person in list) 13 { 14 person.Cut(); 15 } 16 Console.ReadKey(); 17 }