多态-抽象类


抽象类:

当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。

关键字abstract,抽象方法没有方法体

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 _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 }

2.练习

  1 using System;
  2 
  3 namespace Practice_多态_抽象类
  4 {
  5     class Program
  6     {
  7         static void Main(string[] args)
  8         {
  9             Sharp s = new Circle(6);
 10             double perimeter=s.GetPerimeter();
 11             double area=s.GetArea();
 12             Sharp h = new Square(1, 2);
 13             double area1= h.GetArea();
 14             double preimeter1 = h.GetPerimeter();
 15             Console.WriteLine("圆的面积是{0},周长是{1},矩形的面积是{2},周长是{3}", area, perimeter, area1, preimeter1);
 16             Console.ReadKey();
 17             
 18             
 19         }
 20     }
 21 }
 22 
 23 
 24 
 25 
 26 using System;
 27 using System.Collections.Generic;
 28 using System.Linq;
 29 using System.Text;
 30 using System.Threading.Tasks;
 31 
 32 namespace Practice_多态_抽象类
 33 {
 34     public abstract class Sharp
 35     {
 36         public abstract double GetArea();
 37         public abstract double GetPerimeter();
 38     }
 39 
 40 }
 41 
 42 
 43 
 44 using System;
 45 using System.Collections.Generic;
 46 using System.Linq;
 47 using System.Text;
 48 using System.Threading.Tasks;
 49 
 50 namespace Practice_多态_抽象类
 51 {
 52     public class Circle:Sharp
 53     {
 54         private double _r;
 55         public double R
 56         {
 57             get { return _r; }
 58             set { _r = value; }
 59         }
 60         public Circle(double r)
 61         {
 62             this.R = r;
 63         }
 64         public override double GetArea()
 65         {
 66             return Math.PI * this.R * this.R;
 67         }
 68         public override double GetPerimeter()
 69         {
 70             return 2 * Math.PI * this.R;
 71         }
 72     }
 73 }
 74 
 75 
 76 
 77 
 78 using System;
 79 using System.Collections.Generic;
 80 using System.Linq;
 81 using System.Text;
 82 using System.Threading.Tasks;
 83 
 84 namespace Practice_多态_抽象类
 85 {
 86     public class Square:Sharp
 87     {
 88         private double _height;
 89         public double Height
 90         {
 91             get { return _height; }
 92             set { _height = value; }
 93         }
 94         private double _with;
 95         public double With
 96         {
 97             get { return _with; }
 98             set { _with = value; }
 99         }
100         public Square(double with,double height)
101         {
102             this.With = with;
103             this.Height = height;
104         }
105         
106         public override double GetPerimeter()
107         {
108             return this._height * 2 + this._with * 2;
109         }
110         public override double GetArea()
111         {
112             return this._with * this._height;
113         }
114     }
115 }

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 _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(ms);
 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     /// 
 38     /// 抽象的父类
 39     /// 
 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         public Computer(MobileStorage ms)
109         {
110             Ms = ms;
111         }
112     }
113 }