这是典型的课程.
class Round
{
private int radius;
private bool Validate(int radius)
{
if (radius <= 0)
{
return false;
}
else
{
return true;
}
}
public int X { get; set;}
public int Y { get; set;}
public int Radius
{
get
{
return radius;
}
set
{
try
{
if(!Validate(value))
{
throw new ArgumentException();
}
radius = value;
}
catch (ArgumentException)
{
Console.WriteLine("ooops... something went wrong.");
return;
}
}
}
public Round (int X, int Y, int radius)
{
try
{
if (!Validate(radius))
{
throw new ArgumentException();
}
this.X = X;
this.Y = Y;
this.radius = radius;
}
catch (Exception)
{
Console.WriteLine("ooops... something went wrong.");
}
}
public double GetPerimeter()
{
return Math.PI * radius * 2;
}
}
class Program
{
static void Main(string[] args)
{
Round r = new Round(0,0,0);
Console.WriteLine(r.GetPerimeter());
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我向构造函数发送了不正确的值,该值将值发送给验证程序.我无法理解我应该怎么做才能使构造函数停止创建对象???