class Person
{
private int age;
private string name;
public Person(int age, string name)
{
this.age = age;
this.name = name;
}
public int Age
{
set
{
this.age = value;
}
get
{
return this.age;
}
}
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
可以把public int Age中的Age看做也变量
例如我要访问age时,可以这样 , per.Age当赋值时,会自动调取set方法,其中 30就是方法中的value
public static void Main(stringp[] args){
Person per = new Person(20 , "zhangsan");
per.Age = 30;
Console.WriteLine(per.Age);
}