刚学c#,构造函数和属性问题,望高手解答,谢谢
实例化后调用构造函数,但是age字段想要利用属性判断,但是好像不走属性,调用好构造函数就返回了,所以age总是返回0,不知道怎么才能调用属性呢
namespace ConsoleApplication4
{
class Class1
{
private string name;
private int age;
public Class1()
{
}
public Class1(string name,int age)
{
this.name = name;
this.age=this.AgeProp; 知道此句有问题
}
public string NameProp
{
get
{
return this.name;
}
}
public int AgeProp
{
get
{
return this.age;
}
set
{
if (value > 0 && value < 150)
{
this.age = value;
}
else
{
throw new Exception("age must be 0 between 150");
}
}
}
}
}
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Class1("a", 30).AgeProp);
Console.ReadKey();
}
}
}