打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VA15.5 索引器概述及使用

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();

        }

    }

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
统计某个字符串中指定字符串出现的次数
C#索引器(一) - C#教程 - 新客网
Shell排序
数组的常用方法例题,复习数组
C#数据类型
this[int index]
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服