//用面向对象多态的思想分别去求圆形和长方形的面积和周长 static void Main(string[] args) { Sharp sharp = new Circle(5); double area=sharp.GetArea(); double perimeter= sharp.Getperimeter(); Console.WriteLine("面积是{0},周长是{1}",area,perimeter); Console.ReadKey(); } public abstract class Sharp { public abstract double GetArea(); public abstract double Getperimeter(); } public class Circle : Sharp { 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; } } public class Square : Sharp { private double _height; public double Height { get { return _height; } set { _height = value; } } private double _width; public double Width { get { return _width; } set { _width = value; } } public Square(double height, double width) { this.Height = height; this.Width = width; } public override double GetArea() { return this.Height * this.Width; } public override double Getperimeter() { return (this.Height + this.Width)*2; } }
时间: 2024-10-16 00:55:05