//声明一个图形基类Shape,该类仅拥有用于存储Shape面积的属性S。在此基础上派生出Rectangle和Circle
//二者都有计算对象面积的方法GetArea。完成相关类的创建,并在主函数中完成类的实例化及其测试。
using System;
using System.Dynamic;
using System.Security.Cryptography.X509Certificates;
namespace 计算图形面积的抽象类
{
abstract class Shape
{
private float s;
public float S
{
get
{
return s;
}
set
{
if (value >= 0)
{ s = value; }
}
}
}
class Rectangle:Shape
{
public float GetArea(float c,float k)
{
return S=c* k;
}
}
class Circle:Shape
{
static double pi = 3.1415926;
public float GetArea(float r)
{
return S=Convert .ToSingle( pi * r * r);
}
}
class Program
{
static void Main(string[] args)
{
float c,k,r;
Rectangle rectangle = new Rectangle();
Circle circle = new Circle();
Console.WriteLine("请输入长方形的长和宽");
c=Convert.ToSingle(Console.ReadLine());
k=Convert.ToSingle(Console.ReadLine());
Console.WriteLine("长方形的面积为:{0}", rectangle.GetArea(c, k));
Console.WriteLine("请输入圆形的半径");
r = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("圆形的面积为:{0}",circle.GetArea(r));
Console.WriteLine("Hello World!");
}
}
}