zoukankan      html  css  js  c++  java
  • .net初学之索引器

    1.语法
    (可有可无)[访问修饰符]  数据类型  this[数据类型  标识符]
    {
        get{}
        set{}
    }
    e.g:
     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Indexer ind = new Indexer(5);
     6             ind[0] = new animal("猫科", " 豹子");
     7             ind[1] = new animal("猫科", " 豹子1");
     8             ind[2] = new animal("猫科", " 豹子2");
     9             ind[3] = new animal("猫科", " 豹子3");
    10             ind[4] = new animal("猫科", " 豹子4");
    11  
    12             for (int i = 0; i < 5; i++)
    13             {
    14                 Console.WriteLine(ind[i].show());
    15             }
    16         }
    17     }
    18  
    19     public class animal
    20     {
    21         private string kemu;
    22         public string Kemu
    23         {
    24             get { return kemu; }
    25             set { kemu = value; }
    26         }
    27         private string name;
    28         public string Name
    29         {
    30             get { return name; }
    31             set { name = value; }
    32         }
    33         public animal() { }
    34         public animal(string _kemu, string _name)
    35         {
    36             this.Kemu = _kemu;
    37             this.Name = _name;
    38         }
    39         public string show()
    40         {
    41             return "名字" + Name + "科目是" + Kemu;
    42         }
    43     }
    44     public class Indexer
    45     {
    46         public animal[] ani;
    47         public Indexer(int count)
    48         {
    49             if (count < 0)
    50                 Console.WriteLine("长度不合法");
    51             ani = new animal[count];
    52         }
    53  
    54         public animal this[int index]
    55         {
    56             get
    57             {
    58                 if (index > ani.Length)
    59                     return null;
    60                 return ani[index];
    61             }
    62             set { ani[index] = value; }
    63         }
    64     }
  • 相关阅读:
    iOS面试题总结整理(附答案)
    iOS App上传项目遇到的问题
    Could not find Developer Disk Image
    xcode下载方式
    iOS App上架流程(2016详细版)
    Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
    iOS开发之17个常用代码整理
    iOS求职之OC面试题
    Android xmpp 连接基本方法
    Ubuntu安装过程
  • 原文地址:https://www.cnblogs.com/wu-tong/p/5799090.html
Copyright © 2011-2022 走看看