//聲明一個圖形基類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!");
}
}
}