定义一个类,封装矩形的长和宽;在定义一个类,继承自定义的这个类,在继承类中根据基类中封装的矩形的长和宽求矩形的面积。

来源:互联网 发布:js done fail 编辑:程序博客网 时间:2024/06/01 15:39
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();
        }
    }
}
阅读全文
0 0
原创粉丝点击