zoukankan      html  css  js  c++  java
  • Intern Day83

    为什么引入索引器

    一般属性只能访问单一的字段(一个属性对一个字段进行封装),如果想访问多个数据成员,就需要使用索引器。索引器是类的特殊成员,它可以根据索引在多个成员中进行选择,能够让对象以类似数组的方式来存取。而这种方式就叫 索引器

    作用

    1. 索引器(Indexer) 。

    2. 索引器允许类中对象可以像数组一样使用下标的方式来访问数据(更方便、直观的被引用)。(可以当做虚拟数组)

    3. 可以使用数组访问运算符 [ ] 来访问该类的实例。

    规则

    1. 索引器的数据类型必须是统一的

    2. 索引器的行为的声明在某种程度上类似于属性(property)。属性可使用 get 和 set 访问器来定义索引器。但是属性返回或设置的是一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。

    3. 关键字:this。(索引器必须以this关键字定义,this 是类实例化之后的对象)

    注意

    本文只讲了一维索引器,重载索引后续碰到了再写。

    基本语法

    一维索引器的定义:

    [修饰符] 数据类型 this[索引类型 index]
    { 
        get // get 访问器
        {
             // 返回index指定的值,往往跟索引的不同返回不同的字段,用选择语句
        }
        set   // set 访问器
        {
             //设置设置index指定的值(赋值语句)
        }
    }
    

    具体使用例子

    using System;
    using System.Diagnostics;
    using System.Reflection;
    using System.Security.Cryptography;
    
    namespace Practice1 // namespace 索引器
    {
        class Student //  public class Student
        {
            private string _name, _gender,_tel; // 该字段格式:private readonly People _people;
    
            //索引器语法格式:访问修饰符 数据类型 this[索引器类型 index]
            public string this[int index]
            {
                get
                {
                    switch (index)
                    {
                        case 1: return _name; // step10
                        case 2: return _gender; // step11
                        case 3: return _tel; // step2
                        default: throw new ArgumentOutOfRangeException("index is wrong!");//抛出异常
                    }
                }
                set
                {
                    switch (index)
                    {
                        case 1: _name = value;break; // step3
                        case 2: _gender = value; break; // step5
                        case 3: _tel = value; break; // step7
                        // 这边的value关键字:表示从外部接收一个新的值,并将值指向该属性
                        default: throw new ArgumentOutOfRangeException("index is wrong!");//抛出异常
                    }
                }
            }
            public void Show() // step9 进step9的时候去get找每个变量对应的值
            {
                Console.WriteLine("name = {0},gender = {1},tel = {2}", this[1], this[2], this[3]); // step10 11 12
                //this[] this表示的是索引器,this[]表示访问对应字段。
            }
        }
        
        class Program
        {
            static void Main(string[] args)
            {
                Student s = new Student(); // step1
                s[1] = "Nicole"; // step2 //给索引器以数组的方式赋值
                s[2] = "girl"; // step4
                s[3] = "123456789"; // step6
                // 这三个赋值分别走set方法
                
                s.Show(); // step8
                // 输出的时候分别走get方法
                
                Console.ReadKey(); // step13
            }
        }
    }
    
    // 或者也可以单独把Program部分放在上面那个namespace下,其他的放在下面这个namespace里面,效果一样的
    // namespace Practice1 // namespace 索引器
    // {
    //     class Student //  public class A
    //     {
    //         private string _name, _gender,_tel; // private readonly People _people;
    //
    //         //索引器语法格式:访问修饰符 数据类型 this[索引器类型 index]
    //         public string this[int index]
    //         {
    //             get
    //             {
    //                 switch (index)
    //                 {
    //                     case 1: return _name;
    //                     case 2: return _gender;
    //                     case 3: return _tel;
    //                     default: throw new ArgumentOutOfRangeException("index");//抛出异常
    //                 }
    //             }
    //             set
    //             {
    //                 switch (index)
    //                 {
    //                     case 1: _name = value; break;
    //                     case 2: _gender = value; break;
    //                     case 3: _tel = value; break;
    //                     default: throw new ArgumentOutOfRangeException("index");//抛出异常
    //                 }
    //             }
    //         }
    //         public void Show()
    //         {
    //             Console.WriteLine("name = {0},gender = {1},tel = {2}", this[1], this[2], this[3]);
    //             //this[] this表示的是索引器,this[]表示访问向对应的字段。
    //         }
    //     }
    // }
    

    输出:

    name = Nicole,gender = girl,tel = 123456789
    
  • 相关阅读:
    [20170706]SQL Server事务复制订阅端,job不小心被删,修复
    [20170629]带过滤的复制项UI操作导致订阅全部初始化问题
    自动创建数据库镜像,证书交换
    “RESOURCE MONITOR“CPU占用特别高
    索引视图导致死锁
    Percona TokuDB
    从MySQL 5.5迁移到Mariadb 10.1.14
    SQL Server 2014新特性:其他
    SQL Server 2014新特性:分区索引重建
    SQL Server 2012 新特性:其他
  • 原文地址:https://www.cnblogs.com/OFSHK/p/14754685.html
Copyright © 2011-2022 走看看