C# 用控制台应用程序 编写一个矩形类

如题所述

using System;

namespace ConsoleApplication1
{
    // 矩形类
    public class Rectangle
    {
        private float width;
        private float height;
        //构造函数:参数为宽度width和高度height
        public Rectangle(float width, float height)
        {
            this.width = width;
            this.height = height;
        }
        // 获取宽度
        public float Width { get { return this.width; } }
        //获取高度
        public float Height { get { return this.height; } }
        // 获取面积
        public float Area { get { return this.width * this.height; } }
        // 获取周长
        public float Circumference { get { return 2 * (this.width + this.height); } }
        // 用于输出
        public override string ToString()
        {
            return string.Format("矩形的宽度:{0},高度:{1},面积:{2},周长:{3}",
                Width, Height, Area, Circumference);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r1 = new Rectangle(100, 50);
            Console.WriteLine(r1.ToString());

            Rectangle r2 = new Rectangle(30.5f, 67.7f);
            Console.WriteLine(r2.ToString());

            Console.ReadLine();
       }
   }
}

追问

假设图书馆的图书类Book包含书名,编号和作者名属性,读者类Reader包含姓名和借书证属性,每位读者最多可借5本书,设计他们的公共基类BClass,要求列出所有读者的借书情况,,
求代码

追答

;-) it's ANOTHER question, isn't it?

追问

yes,please help me!

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-06-20
 public class Rectangle
    {
        public Rectangle(int width, int height)
        {
            this.Width = width;
            this.Height = height;
        }
        public int Width { get; set; }
        public int Height { get; set; }
        public double area { get => Width * Height; }
    }

本回答被网友采纳
相似回答