class class1 { public static void Main(string[] args) { //使用多态求矩形面积与周长和圆的面积与周长 Shape cl = new Circle(5); double clarea = cl.GetArea(); double clpar = cl.GetPerimeter(); Console.WriteLine("这个圆的面积是{0},周长是{1}", Math.Round(clarea, 2), Math.Round(clpar, 2)); Shape sq = new Square(5, 10); double sqarea = sq.GetArea(); double sqpar = sq.GetPerimeter(); Console.WriteLine("这个方形的面积是{0},周长是{1}", Math.Round(sqarea, 2), Math.Round(sqpar, 2)); Console.ReadKey(); } } //声明一个抽像类 public abstract class Shape { public abstract double GetArea(); public abstract double GetPerimeter(); } class Circle:Shape { private double _r; public double R { get { return _r; } set { _r = value; } } public Circle(double r) { this.R = r; } public override double GetArea() { return Math.PI * this.R * this.R; } public override double GetPerimeter() { return 2 * Math.PI * this.R; } } class Square : Shape { private double _height; private double _width; public double Height { get { return _height; } set { _height = value; } } public double Width { get { return _width; } set { _width = value; } } public Square(double w, double h) { this.Height = h; this.Width = w; } public override double GetArea() { return this.Width * this.Height; } public override double GetPerimeter() { return (this.Width + this.Height) * 2; } }