using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 抽象类_抽象方法_多态性的实现
{
public abstract class Shape
{
protected string name;
public Shape(string name)
{
this.name = name;
}
public abstract void Show();
public abstract double Area();
}
public class Rectangle : Shape
{
protected double weight;
protected double height;
public Rectangle(string name, double w, double h)
: base(name)
{
this.weight = w;
this.height = h;
}
public override void Show()
{
Console.WriteLine("Rectangle:{0},area:{1}", name, weight * height);
}
public override double Area()
{
return weight * height;
}
}
public class Circle : Shape
{
protected double radius;
public Circle(string name, double r)
: base(name)
{
this.radius = r;
}
public override void Show()
{
Console.WriteLine("Circle:{0},area:{1}", name, Math.PI * radius * radius);
}
public override double Area()
{
return Math.PI * radius * radius;
}
}
class Program
{
static void Main(string[] args)
{
Shape[] s = { new Rectangle("小矩形", 1.0, 2.0), new Circle("大圆", 3.5) };
foreach (Shape e in s)
{
e.Show();
}
Console.ReadKey();
}
}
}