摘录自:
team-learning-program
public class Rectangle
{
public float Length;
public float Width;
public Rectangle(float length, float width)
{
Length = length > 0 ? length : 0f;
Width = width > 0 ? width : 0f;
}
public void SetLength(float length)
{
Length = length > 0 ? length : 0f;
}
public void SetWidth(float width)
{
Width = width > 0 ? width : 0f;
}
public float GetArea()
{
if (Length * Width == 0)
throw new ArgumentOutOfRangeException("长度或宽度为零。");
return Length * Width;
}
public float GetPerimeter()
{
if (Length * Width == 0)
throw new ArgumentOutOfRangeException("长度或宽度为零。");
return (Length + Width) * 2;
}
}