namespace clo
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person("Smile");

            p1["telPhoneNumber"] = "0000-112233";
            p1["Address"] = "ide-cd-#12";

            p1.showMessage();
        }
    }

    class Person
    {
        //定义一个人的姓名,手机号码,家庭住址
        private string Name;    
        private string telPhoneNumber;        
        private string Address;

        public Person() { }
        public Person(string name) { this.name = name; }

        public string name
        {
            set;
            get;
        }

        //定义一个索引器
        public string this[string type]
        {
            get
            {
               switch(type.ToLower())
                {
                   //因为将type ToLower了
                   case "telphonenumber":
                        return this.telPhoneNumber;
                   case "address":
                        return this.Address;
                   default:
                        return null;
                }
            }
            set
            {
                switch (type.ToLower())
                {
                    case "telphonenumber":
                        this.telPhoneNumber = value;
                        break;
                    case "address":
                        this.Address = value;
                        break;
                }
            }
        }

        //打印出信息
        public void showMessage()
        {
            Console.WriteLine("My Name is {0},TelPhoneNumber is {1} and HomeAddress is {2}", this.name, this.telPhoneNumber, this.Address);
        }
    }
}

 

Posted on 2013-05-15 11:18  SmileCN  阅读(131)  评论(0编辑  收藏  举报