属性的四个种类:
(1)可读写(Read/Write)属性,需实现get和set访问器
(2)只读(Readonly)属性,只实现get访问器
(3)只写(Writeonly)属性,只实现set访问器
(4)Static属性,只能访问静态数据
using System; public class Prop { private string m_name; private string m_sex; //属性Name,读取和设置m_name public string Name { get { return m_name; } set { m_name = value;//value隐含参数,由CLR自动生成 } } //属性Sex,读取和设置m_sex public string Sex { get { return m_sex; } set { m_sex = value; } } } public class Test { static void Main() { Prop p = new Prop(); p.Sex = "男"; p.Name = "牛大"; Console.WriteLine(p.Name); Console.WriteLine(p.Sex); } }