1、一个多态例子
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _09多态 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //概念:让一个对象能够表现出多种的状态(类型) 14 //实现多态的3种手段:1、虚方法 2、抽象类 3、接口 15 16 Chinese cn1 = new Chinese("韩梅梅"); 17 Chinese cn2 = new Chinese("李雷"); 18 Japanese j1 = new Japanese("树下君"); 19 Japanese j2 = new Japanese("井边子"); 20 Korea k1 = new Korea("金秀贤"); 21 Korea k2 = new Korea("金贤秀"); 22 American a1 = new American("科比布莱恩特"); 23 American a2 = new American("奥尼尔"); 24 Person[] pers = { cn1, cn2, j1, j2, k1, k2, a1, a2, new English("格林"), new English("玛利亚") }; 25 26 for (int i = 0; i < pers.Length; i++) 27 { 28 //if (pers[i] is Chinese) 29 //{ 30 // ((Chinese)pers[i]).SayHello(); 31 //} 32 //else if (pers[i] is Japanese) 33 //{ 34 // ((Japanese)pers[i]).SayHello(); 35 //} 36 //else if (pers[i] is Korea) 37 //{ 38 // ((Korea)pers[i]).SayHello(); 39 //} 40 //else 41 //{ 42 // ((American)pers[i]).SayHello(); 43 //} 44 45 46 pers[i].SayHello(); 47 } 48 Console.ReadKey(); 49 } 50 } 51 52 public class Person 53 { 54 private string _name; 55 public string Name 56 { 57 get { return _name; } 58 set { _name = value; } 59 } 60 61 public Person(string name) 62 { 63 this.Name = name; 64 } 65 public virtual void SayHello() 66 { 67 Console.WriteLine("我是人类"); 68 } 69 70 } 71 72 public class Chinese : Person 73 { 74 public Chinese(string name) 75 : base(name) 76 { 77 78 } 79 80 public override void SayHello() 81 { 82 Console.WriteLine("我是中国人,我叫{0}", this.Name); 83 } 84 } 85 public class Japanese : Person 86 { 87 public Japanese(string name) 88 : base(name) 89 { } 90 91 public override void SayHello() 92 { 93 Console.WriteLine("我是脚盆国人,我叫{0}", this.Name); 94 } 95 } 96 public class Korea : Person 97 { 98 public Korea(string name) 99 : base(name) 100 { 101 102 } 103 104 105 public override void SayHello() 106 { 107 Console.WriteLine("我是棒之思密达,我叫{0}", this.Name); 108 } 109 } 110 public class American : Person 111 { 112 public American(string name) 113 : base(name) 114 { 115 116 } 117 118 public override void SayHello() 119 { 120 Console.WriteLine("我叫{0},我是米国人", this.Name); 121 } 122 } 123 124 125 public class English : Person 126 { 127 public English(string name) 128 : base(name) 129 { } 130 131 public override void SayHello() 132 { 133 Console.WriteLine("我是英国人"); 134 } 135 } 136 137 }
2、多态练习
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10多态练习 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //真的鸭子嘎嘎叫 木头鸭子吱吱叫 橡皮鸭子唧唧叫 14 //RealDuck rd = new RealDuck(); 15 //rd.Bark(); 16 //MuDuck md = new MuDuck(); 17 //md.Bark(); 18 //XPDuck xd = new XPDuck(); 19 //xd.Bark(); 20 //Console.ReadKey(); 21 22 //RealDuck rd = new RealDuck(); 23 //MuDuck md = new MuDuck(); 24 //XPDuck xd = new XPDuck(); 25 //RealDuck[] ducks = { rd, md, xd }; 26 //for (int i = 0; i < ducks.Length; i++) 27 //{ 28 // ducks[i].Bark(); 29 //} 30 //Console.ReadKey(); 31 32 33 //经理十一点打卡 员工9点打卡 程序猿不打卡 34 //Employee emp = new Employee(); 35 //Manager mg = new Manager(); 36 //Programmer pm = new Programmer(); 37 //Employee[] emps = { emp, mg, pm }; 38 //for (int i = 0; i < emps.Length; i++) 39 //{ 40 // emps[i].DaKa(); 41 //} 42 //Console.ReadKey(); 43 44 45 46 //狗狗会叫 猫咪也会叫 47 48 } 49 } 50 51 public class Animal 52 { 53 public void Bark() 54 { 55 56 } 57 } 58 59 60 public class Employee 61 { 62 public virtual void DaKa() 63 { 64 Console.WriteLine("九点打卡"); 65 } 66 } 67 68 public class Manager : Employee 69 { 70 public override void DaKa() 71 { 72 Console.WriteLine("经理11点打卡"); 73 } 74 } 75 76 public class Programmer : Employee 77 { 78 public override void DaKa() 79 { 80 Console.WriteLine("程序猿不打卡"); 81 } 82 } 83 84 85 86 87 88 public class RealDuck 89 { 90 public virtual void Bark() 91 { 92 Console.WriteLine("真的鸭子嘎嘎叫"); 93 } 94 } 95 96 public class MuDuck : RealDuck 97 { 98 public override void Bark() 99 { 100 Console.WriteLine("木头鸭子吱吱叫"); 101 } 102 } 103 104 public class XPDuck : RealDuck 105 { 106 public override void Bark() 107 { 108 Console.WriteLine("橡皮鸭子唧唧叫"); 109 } 110 } 111 }
3、抽象类的两个例子
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11_抽象类 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //狗狗会叫 猫咪会叫 14 15 Animal a = new Cat();//new Dog(); 16 a.Bark(); 17 18 Console.ReadKey(); 19 } 20 } 21 22 public abstract class Animal 23 { 24 25 public virtual void T() 26 { 27 Console.WriteLine("动物有声明"); 28 } 29 30 private int _age; 31 32 public int Age 33 { 34 get { return _age; } 35 set { _age = value; } 36 } 37 38 public Animal(int age) 39 { 40 this.Age = age; 41 } 42 public abstract void Bark(); 43 public abstract string Name 44 { 45 get; 46 set; 47 } 48 49 // public abstract string TestString(string name); 50 51 52 public Animal() 53 { 54 55 } 56 //public void Test() 57 //{ 58 // //空实现 59 //} 60 } 61 62 63 public abstract class Test : Animal 64 { 65 66 } 67 68 public class Dog : Animal 69 { 70 // public abstract void Test(); 71 72 73 public override void Bark() 74 { 75 Console.WriteLine("狗狗旺旺的叫"); 76 } 77 78 public override string Name 79 { 80 get 81 { 82 throw new NotImplementedException(); 83 } 84 set 85 { 86 throw new NotImplementedException(); 87 } 88 } 89 90 //public override string TestString(string name) 91 //{ 92 // //throw new NotImplementedException(); 93 //} 94 } 95 96 public class Cat : Animal 97 { 98 public override void Bark() 99 { 100 Console.WriteLine("猫咪喵喵的叫"); 101 } 102 103 public override string Name 104 { 105 get 106 { 107 throw new NotImplementedException(); 108 } 109 set 110 { 111 throw new NotImplementedException(); 112 } 113 } 114 } 115 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12_抽象类 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //使用多态求矩形的面积和周长以及圆形的面积和周长 14 Shape shape = new Square(5, 6); //new Circle(5); 15 double area = shape.GetArea(); 16 double perimeter = shape.GetPerimeter(); 17 Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter); 18 Console.ReadKey(); 19 20 } 21 } 22 23 public abstract class Shape 24 { 25 public abstract double GetArea(); 26 public abstract double GetPerimeter(); 27 } 28 public class Circle : Shape 29 { 30 31 private double _r; 32 public double R 33 { 34 get { return _r; } 35 set { _r = value; } 36 } 37 38 public Circle(double r) 39 { 40 this.R = r; 41 } 42 public override double GetArea() 43 { 44 return Math.PI * this.R * this.R; 45 } 46 47 public override double GetPerimeter() 48 { 49 return 2 * Math.PI * this.R; 50 } 51 } 52 public class Square : Shape 53 { 54 private double _height; 55 56 public double Height 57 { 58 get { return _height; } 59 set { _height = value; } 60 } 61 62 private double _width; 63 64 public double Width 65 { 66 get { return _width; } 67 set { _width = value; } 68 } 69 70 public Square(double height, double width) 71 { 72 this.Height = height; 73 this.Width = width; 74 } 75 76 public override double GetArea() 77 { 78 return this.Height * this.Width; 79 } 80 81 public override double GetPerimeter() 82 { 83 return (this.Height + this.Width) * 2; 84 } 85 } 86 87 }
4、用多态来实现 将 移动硬盘或者U盘或者MP3插到电脑上进行读写数据
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13_电脑_移动硬盘_U盘_MP3 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //用多态来实现 将 移动硬盘或者U盘或者MP3插到电脑上进行读写数据 14 15 //MobileDisk md = new MobileDisk(); 16 //UDisk u = new UDisk(); 17 //Mp3 mp3 = new Mp3(); 18 //Computer cpu = new Computer(); 19 //cpu.CpuRead(u); 20 //cpu.CpuWrite(u); 21 //Console.ReadKey(); 22 23 MobileStorage ms = new UDisk();//new Mp3();//new MobileDisk();//new UDisk(); 24 Computer cpu = new Computer(); 25 cpu.Ms = ms; 26 cpu.CpuRead(); 27 cpu.CpuWrite(); 28 //Computer cpu = new Computer(); 29 //cpu.CpuRead(ms); 30 //cpu.CpuWrite(ms); 31 Console.ReadKey(); 32 33 } 34 } 35 36 37 /// <summary> 38 /// 抽象的父类 39 /// </summary> 40 public abstract class MobileStorage 41 { 42 public abstract void Read(); 43 public abstract void Write(); 44 } 45 46 47 public class MobileDisk : MobileStorage 48 { 49 public override void Read() 50 { 51 Console.WriteLine("移动硬盘在读取数据"); 52 } 53 public override void Write() 54 { 55 Console.WriteLine("移动硬盘在写入数据"); 56 } 57 } 58 public class UDisk : MobileStorage 59 { 60 public override void Read() 61 { 62 Console.WriteLine("U盘在读取数据"); 63 } 64 65 public override void Write() 66 { 67 Console.WriteLine("U盘在写入数据"); 68 } 69 } 70 public class Mp3 : MobileStorage 71 { 72 public override void Read() 73 { 74 Console.WriteLine("MP3在读取数据"); 75 } 76 77 public override void Write() 78 { 79 Console.WriteLine("Mp3在写入数据"); 80 } 81 82 public void PlayMusic() 83 { 84 Console.WriteLine("MP3自己可以播放音乐"); 85 } 86 } 87 88 89 90 public class Computer 91 { 92 private MobileStorage _ms; 93 94 public MobileStorage Ms 95 { 96 get { return _ms; } 97 set { _ms = value; } 98 } 99 public void CpuRead() 100 { 101 Ms.Read(); 102 } 103 104 public void CpuWrite() 105 { 106 Ms.Write(); 107 } 108 } 109 }