设计并测试一个名为Retangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积

2025-06-22 19:50:53
推荐回答(1个)
回答1:

public class Rectangle
{
private Point _bottomLeft = Point.Empty;
private Point _topRight = Point.Empty;

public Point BottomLeft
{
get
{
return this._bottomLeft;
}
set
{
this._bottomLeft = value;
}
}

public Point TopRight
{
get
{
return this._topRight;
}
set
{
this._topRight = value;
}
}

public int GetArea()
{
if (this._bottomLeft == this._topRight)
{
return 0;
}

int width = this._topRight.X - this._bottomLeft.X;
int heigth = this._bottomLeft.Y - this._topRight.Y;

return width * heigth;
}