C#抽象类和抽象方法的实现
C#抽象类和抽象方法的实现
抽象类是一种特殊的基础类,并不与具体的事物联系。抽象类的定义使用关键字abstract。
在类的层次结构中,并没有“图形”这样的具体事物,所以可以将“图形”定义为抽象类,派生出“圆形”和“四边形”这样一些可以具体实例化的普通类,需要注意的是,抽象类不能被实例化,他只能作为其他类的基础类。将Shape类定位为抽象类代码如下:
public absract class shape
{
.....
}
在抽象类中也可以使用关键字absract定义抽象方法,要求所有的派生非抽象类都要重载实现抽象方法,引入抽象方法的原因在于抽象类本身是一种抽象概念,有的方法并不需要具体实现,而是留下来让派生类重载实现。Shape类中GetArea方法本身并无具体意义,而只有到了派生类Cricle和Reatangular才可以计算具体面积。
抽象方法为:
public absract double GetArea();
则派生类重载实现为:
public override double GetArea();
{
......
}
下面我们用具体的工程案例讲解:---------------------------------------->
首先我们在工程文件中添加一个类 Shape类-------Shape.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Application27
7 {
8 //定义基类Shape
9 public abstract class Shape
10 {
11 protected string Color;
12 public Shape() { ;} //构造函数
13 public Shape(string Color) { this.Color = Color; }
14 public string GetColor() { return Color; }
15 public abstract double GetArea(); //抽象类
16 }
17 //定义Cirle类,从Shape类中派生
18 public class Circle : Shape
19 {
20 private double Radius;
21 public Circle(string Color, double Radius)
22 {
23 this.Color = Color;
24 this.Radius = Radius;
25 }
26 public override double GetArea()
27 {
28 return System.Math.PI * Radius * Radius;
29 }
30
31 }
32 //派生类Rectangular,从Shape类中派生
33 public class Retangular : Shape
34 {
35 protected double Length, Width;
36 public Retangular(string Color, double Length, double Width)
37 {
38 this.Color = Color;
39 this.Length = Length;
40 this.Width = Width;
41 }
42 public override double GetArea()
43 {
44 return (Length*Width);
45 }
46
47 public double PerimeterIs()
48 {
49 return (2 * (Length * Width));
50
51 }
54 }
55 //派生类Square,从Retangular类中派生
56 public class Square : Retangular
57 {
58 public Square(string Color,double Side):base(Color,Side,Side) { ;}
59
60
61 }
65 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Application27 7 { 8 //定义基类Shape 9 public abstract class Shape 10 { 11 protected string Color; 12 public Shape() { ;} //构造函数 13 public Shape(string Color) { this.Color = Color; } 14 public string GetColor() { return Color; } 15 public abstract double GetArea(); //抽象类 16 } 17 //定义Cirle类,从Shape类中派生 18 public class Circle : Shape 19 { 20 private double Radius; 21 public Circle(string Color, double Radius) 22 { 23 this.Color = Color; 24 this.Radius = Radius; 25 } 26 public override double GetArea() 27 { 28 return System.Math.PI * Radius * Radius; 29 } 30 31 } 32 //派生类Rectangular,从Shape类中派生 33 public class Retangular : Shape 34 { 35 protected double Length, Width; 36 public Retangular(string Color, double Length, double Width) 37 { 38 this.Color = Color; 39 this.Length = Length; 40 this.Width = Width; 41 } 42 public override double GetArea() 43 { 44 return (Length*Width); 45 } 46 47 public double PerimeterIs() 48 { 49 return (2 * (Length * Width)); 50 51 } 54 } 55 //派生类Square,从Retangular类中派生 56 public class Square : Retangular 57 { 58 public Square(string Color,double Side):base(Color,Side,Side) { ;} 59 60 61 } 65 }
这里我们用了abstract定以Shape , 即抽象类
然后我们在主程序中设置
-----------------------Program.cs------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Application27 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Circle Cir = new Circle("Orange", 3.0); 13 Console.WriteLine("Circle area is{1}",Cir.GetColor(),Cir.GetArea()); 14 Retangular Rect = new Retangular("Red",13.0,2.0); 15 Console.WriteLine("Retangular Color is {0},Rectangualr area is {1},Rectangualr Perimeter is {2}", 16 Rect.GetColor(),Rect.GetArea(),Rect.PerimeterIs()); 17 Square Squ = new Square("qreen",5.0); 18 Console.WriteLine("Square Color is {0},Square Area is {1},Square Perimeter is {2}",Squ.GetColor(),Squ.GetArea(),Squ.PerimeterIs()); 19 } 20 } 21 }
结果显示如下: