创建一个矩形类,包含长、宽属性和求面积方法。类中包含一个静态Count属性,当创建一个矩形时,Count++。
使用静态构造函数为静态属性赋初值。
在测试类中定义两个方法(static):
(1)判断矩形是否为正方形
(2)扩大矩形,调整矩形的长和宽
在测试类中创建一个矩形,并判断矩形是否为正方形扩大矩形,显示矩形的长宽和面积。
namespace ConsoleApp2 { class Program { static void Main(string[] args) { Rectangle rect2 = new Rectangle() { Width = 4, Height = 4 }; Rectangle.Count++; Console.WriteLine(rect2.Height + "," + rect2.Width); bool result = IsSquare(rect2); ScaleRect(rect2); Console.WriteLine(result); Console.WriteLine(rect2.Height + "," + rect2.Width); Console.WriteLine("矩形的面积是{0}", rect2.Area()); Console.ReadLine(); } static bool IsSquare(Rectangle rect) { if (rect.Width == rect.Height) { return true; } else { return false; } } static void ScaleRect(Rectangle rect) { rect.Width = 10; rect.Height = 12; } public class Rectangle { static public int Count { set; get; } public int Width { set; get; } public int Height { set; get; } public int Area() { return Width * Height; } } } }