1. 程式人生 > >定義一個類,封裝矩形的長和寬;在定義一個類,繼承自定義的這個類,在繼承類中根據基類中封裝的矩形的長和寬求矩形的面積。

定義一個類,封裝矩形的長和寬;在定義一個類,繼承自定義的這個類,在繼承類中根據基類中封裝的矩形的長和寬求矩形的面積。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 封裝長方體
{
    class Rectangle
    {
        protected double width;
        protected double height;


        public double Width
        {
            get { return this.width; }
            set { this.width = value; }
        }
        public double Height
        {
            get { return this.height; }
            set { this.height = value; }
        }
        public double getArea()
        {
             return this.height * this.width; 
        }
        public void display()
        {
            Console.WriteLine("長度:{0}", height);
            Console.WriteLine("寬度:{0}", width );
            Console.WriteLine("面積:{0}", getArea ());


        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Width = 4.5;
            r.Height = 3.5;
            r.display();
            Console.ReadKey();
        }
    }
}