分享

VA15.5 索引器概述及使用

 时间剧毒 2019-04-29

using System;

using System.Collections.Generic;

using System.Text;

namespace VA15.__索引器概述及使用

{

    class Program

    {

        public class indexText //访问类实例

        {

            private int[] array = new int[10];

            public int this[int index]

            {

                get

                {

                    if (index < 0 || index >= 10) return 0;

                    else return array[index];

                }

                set

                {

                    if (index >= 0 && index < 10)

                    {

                        array[index] = value;

                    }

                }

            }

        }

        public class weekIndex { //访问类成员

                string[] Week ={"星期一","星期二","星期三","星期四","星期五","星期六","星期日" };

                private int getDay(string weekText) {

                        int i = 0;

                        foreach (string day in Week ){

                            //如果传入的参数日期等于Week数组中的日期  返回其索引值

                            if (day == weekText) { return i; }

                            i++;

                        }

                        return -1;

                }

                //  属性

                public int this[string week] {

                    get {return getDay(week);}   

                }

        }

        static void Main(string[] args)

        {

            Console.WriteLine("访问类实例的结果");

            indexText Arr = new indexText();

            Arr[-5] = 5;

            Arr[0] = 15;

            Arr[1] = 30;

            Arr[2] = 60;

            Arr[11] = 65;

            Console.WriteLine("Arr[-5]={0}", Arr[-5]);

            Console.WriteLine("Arr[0]={0}", Arr[0]);

            Console.WriteLine("Arr[1]={0}", Arr[1]);

            Console.WriteLine("Arr[2]={0}", Arr[2]);

            Console.WriteLine("Arr[11]={0}", Arr[11]);

            Console.WriteLine("访问类实例的结果");

            weekIndex we = new weekIndex();

            Console.WriteLine(we["星期一"]);

            Console.WriteLine(we["星期三"]);

            Console.WriteLine(we["星期四"]);

            Console.WriteLine(we["星期八"]);

            Console.ReadKey();

        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多