C#中类的属性

数据类型     属性名

{     

       get{

                  return  表达式1;         }

       set{

                表达式2;}

}

  class Class1
    {
        private int x;
        public int attc
        {
            get { return x; }
            set { x = value; }
        }
    }
class Program
{
static void Main(string[] args)
{
Class1 a = new Class1();
a.attc= 100000000;
Console.WriteLine(a.attc);
Console.ReadLine();
}
}


使用属性获取其他类中的私有成员时,只需要保证属性中有get和set这两个访问器即可,a.attc.get();或者a.attc.set();都是错误的。

猜你喜欢

转载自blog.csdn.net/s_p_y_s/article/details/78073828