IndexSelector

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

namespace IndexSelectorConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Month mon = new Month(5);
            Console.WriteLine(mon[1]);
            Console.WriteLine(mon[-1]);
            mon[5] = "五月";
            Console.WriteLine(mon[-1]);
            Console.WriteLine(mon[6]);
        }

        //January
        //May
        //五月
        //June
        //请按任意键继续. . .
    }


    class Month
    {
        private String[] months = new String[] { 
            "January", "February", "March",
            "April","May", "June",
            "July","August","September",
            "October","November","December"
        };
        private int i_mon=1;

        public Month(int _mon)
        {
            if (_mon > 0 && _mon <= months.Length)
            {
                i_mon = _mon;
            }
            
        }

        public String this[int key]
        {
            get
            {
                if (key > 0 && key <= months.Length)
                {
                    return months[key-1];
                } 
                else
                {
                    return months[i_mon-1]; ;
                }
                
            }
            set
            {
                if (key > 0 && key <= months.Length)
                {
                    months[key - 1] = value;
                } 
            }
        }
    }
}

 

posted @ 2017-10-30 11:06  sky20080101  阅读(44)  评论(0)    收藏  举报