第二节 5属性2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


//Red Gate's Reflector 反编译工具
namespace _5属性2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Person5 p = new Person5();
            //p.Age = 30;
            //Console.WriteLine(p.Age);

            //Person6 p = new Person6();
            //p.IncAge();
            //p.IncAge();
            //Console.WriteLine("年龄{0}",p.Age);

            Person7 p = new Person7();
            p.Age = 30;
            Console.WriteLine(p.Age);

            
            Console.ReadKey();
        }
    }

    class Person7 
    {
        public int Age
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
    }

    class Person6
    {
        private int age;
        public int Age //只读属性,没有赋值操作
        {
            get { return age; }
        }
        public void IncAge() 
        {
            age++;
        }
    }

    class Person5 
    {
        private int age;
        public int Age {
            set {
                this.Age = value; //这里易错,列循环,如果直接把value值赋给this.Age的话,
                //因为this.Age就是自己,然后就死循环了
            }
            get {
                return this.Age;
            }
        }
    }
}

  

posted @ 2012-02-23 23:53  简单--生活  阅读(181)  评论(0编辑  收藏  举报
简单--生活(CSDN)