1.将原来班级类中的数组属性Student[]改为Students对象;
2.添加一个Students类并加入索引器,然后将它的访问方式重载,使用姓名进行访问(注意:此时班级类中已经没有了学员类型的数组,所以要在Students类中定义学员类型的数组作为属性)
Student[]改为Students对象;
怎么改:public MyClass(string name)
{
this.name = name;
this.stundents = new Student[3];
stundents[0] = new Student("scofiel", 28, "越狱狱", genders.Female);
stundents[1] = new Student("张靓靓", 20, "唱歌歌", genders.Female);
stundents[2] = new Student("周杰杰", 21, "耍双节棍棍", genders.Male);
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private Student[] stundents;
public Student[] Stundents
{
get { return stundents; }
set { stundents = value; }
}
二。添加一个Students类并加入索引器,然后将它的访问方式重载,怎么做?Students是在Myclass中添加Students类,还是另外添加一个Students,请帮我解释一下,谢谢谢谢…………在线等候
public class StudentCollection
{
private Dictionary<string,Student> m_studentDic;
public StudentCollection(Student[] studentArray)
{
int count = studentArray.Length;
count = count < 37 ? 37 : count;
this.m_studentDic=new Dictionary<string,Student>(count);
foreach (Student student in studentArray)
{
this.m_studentDic.Add(student.Name, student);
}
}
public Student this[string name]
{
get
{
Student student=null;
this.m_studentDic.TryGetValue(name, out student);
return student;
}
}
public void Add(Student student)
{
//添加学生
}
public void Remove(Student student)
{
//删除学生
}
}
按自己的习惯会另外添加一个StudentCollection(Students)类,至于原因,觉得这个类在语义上不仅仅在班级类中有用,可以是班级中学生组合,也可以是来表示89年出生的学生组合,有很多地方可以用到。
1 class MysList : IEnumerable//IEnumerable实现迭代
2 {
3
4 public IEnumerator GetEnumerator()
5 {
6 for (int i = 0; i < emcount; i++)
7 {
8 yield return objarr[i];//yield return是迭代
9 }
10 }
11 object[] objarr = new object[0];
12 int emcount;
13
14 public object this[int index]//索引器//和属性长的相似,有参数,无名称//this是对象本身
15 {
16 get
17 {
18 return objarr[index];
19 }
20 set
21 {
22 objarr[index] = value;
23 }
24 }
25 public int Capacity
26 {
27 get
28 {
29 return objarr.Length;
30 }
31 set
32 {
33 if (value < emcount)
34 {
35 throw new Exception("容量小于数组个数");
36 }
37 else
38 {
39 objarr = new object[value];
40 }
41 }
42 }//容量
43 public int Count
44 {
45 get
46 {
47 return emcount;
48 }
49 }
50 public void Add(object o)
51 {
52 if (objarr.Length == emcount)
53 {
54 if (emcount == 0)
55 {
56 objarr = new object[4];
57 }
58 else
59 {
60 object[] newobjarr = new object[objarr.Length * 2];
61 for (int i = 0; i < objarr.Length; i++)
62 {
63 newobjarr[i] = objarr[i];
64 }
65 objarr = newobjarr;
66 }
67 }
68 objarr[emcount] = o;
69 emcount++;
70 }
71 public void RemoveAt(int index)
72 {
73 object[] newobjarr = new object[objarr.Length];
74 if (index < 0 || index > emcount-1)
75 {
76 throw new Exception("超出数组索引");
77 }
78 for (int i = 0; i < index; i++)
79 {
80 newobjarr[i] = objarr[i];
81 }
82 for (int i = index; i < emcount; i++)
83 {
84 newobjarr[i] = objarr[i + 1];
85 }
86 objarr = newobjarr;
87 emcount--;
88 }
89 public void Remove(object item)
90 {
91 int indexo = IndexOf(item);
92 RemoveAt(indexo);
93 }
94 public void RemoveAll(object item)
95 {
96 while (Exists(item))
97 {
98 int indexo = IndexOf(item);
99 RemoveAt(indexo);
100 }
101 }
102 public int IndexOf(object item)
103 {
104 if (!Exists(item))
105 {
106 throw new Exception("不存在");
107 }
108 int indexo=0;
109 for (int i = 0; i <=emcount+1; i++)
110 {
111 if (objarr[i].ToString() == item.ToString())
112 {
113 return i;
114 }
115 }
116 return indexo;
117 }
118 public bool Exists(object item)
119 {
120 for (int i = 0; i <= emcount + 1; i++)
121 {
122 if (objarr[i].ToString() == item.ToString())
123 {
124 return true;
125 }
126 }
127 return false;
128 }
129
130 }