namespace PropertyExamples
{
class Program
{
static void Main(string[] args)
{
try
{
Student stu1 = new Student();
stu1.SetAge(20);
Student stu2 = new Student();
stu2.SetAge(20);
Student stu3 = new Student();
stu3.SetAge(200);
int avgAge = (stu1.GetAge() + stu2.GetAge() + stu3.GetAge() / 3);
Console.WriteLine(avgAge);
}
catch (Exception wx)
{
Console.WriteLine(wx.Message);
}
}
}
class Student
{
private int age;
public int GetAge()
{
return this.age; // 请看这里
}
public void SetAge(int value)
{
if (value>=0 && value <= 120)
{
this.age = value; // 请看这里
}
else
{
throw new Exception("Age value has error");
}
}
}
}
{
class Program
{
static void Main(string[] args)
{
try
{
Student stu1 = new Student();
stu1.SetAge(20);
Student stu2 = new Student();
stu2.SetAge(20);
Student stu3 = new Student();
stu3.SetAge(200);
int avgAge = (stu1.GetAge() + stu2.GetAge() + stu3.GetAge() / 3);
Console.WriteLine(avgAge);
}
catch (Exception wx)
{
Console.WriteLine(wx.Message);
}
}
}
class Student
{
private int age;
public int GetAge()
{
return this.age; // 请看这里
}
public void SetAge(int value)
{
if (value>=0 && value <= 120)
{
this.age = value; // 请看这里
}
else
{
throw new Exception("Age value has error");
}
}
}
}