C# 用控制台应用程序 编写一个矩形类
2个回答
展开全部
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?
展开全部
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; }
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询