标签:
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 }
标签:
原文地址:http://www.cnblogs.com/wu-tong/p/5799090.html