实验一写个 rectangle(长方形)类,具体要求如下;
1.它具有私有数据成员mdth(宽度)、 height(度)、1(周长),s(面积),。
2.设置一个构造函数,用于在创建rectangle对象时,初使化该对象的长度和宽度。
3.设置求长方形面积的方法area() ,求周长的方法perimeter(),输出面积和周长结果的方法showinfo()。
4.最后,建一个 rectangle类的实例对象 recti,长度设置为10.宽度设置为5.并输出该对象的面积和周长

c#,编写一个rectangle类。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CSDN专家-sinJack 2021-11-18 08:55关注
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _1 { class rectangle { private double width, height, S, L; public rectangle(double w, double h) { width = w; height = h; double S = width * height; double L = 2 * (width + height); } public double w{get;set;} public double h{ get; set;} public double GetArea() { return width * height; } public double GetPerimeter() { return 2 * (width + height); } public string Showinfo() { return string.Format("矩形的面积为:{0},周长为:{1}", S, L); } class Program { static void Main(string[] args) { rectangle rect1 = new rectangle(10, 5); Console.WriteLine("矩形rect1的面积为:{0}", rect1.GetArea()); Console.WriteLine("矩形rect1的周长为:{0}", rect1.GetPerimeter()); Console.Read(); } } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 6无用