計算圖形面積的抽象類--C#

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章