码迷,mamicode.com
首页 > Web开发 > 详细

.net初学之索引器

时间:2016-08-23 14:46:18      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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     }

 

.net初学之索引器

标签:

原文地址:http://www.cnblogs.com/wu-tong/p/5799090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!