属性的定义和使用
属性由两个部分组成:属性头和存储器。存储器分为get访问器和set访问器。声明属性的一般形式为:
修饰符 学校名称不可修改
private string stuName
= "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family:
宋体; ">阿会楠";
private int stuAge
= 22;
public string studentName
{
get { return stuName;
}
set { stuName = value; }
}
public int studentAge
{
get { return stuAge;
}
set { stuAge = value; }
}
public string studentCollege
{
get { return stuCollege;
}
}
public string studentInfo
{
get { return "<span
courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">学校:" +
stuCollege + "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size:
9pt; font-family: 宋体; ">名字:" + stuName + "<span courier=""
new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">岁数:" +
stuAge;}
}
}
class Program
{
static void Main(string[]
args)
{
Student stu = new Student();
Console.Write(stu.studentCollege + "\n");
stu.studentAge = 25;
Console.Write(stu.studentInfo);
Console.ReadKey();
}
}
}