已知学生课程分数类,源代码如下。

jw634595253 2010-11-12 09:37:10



已知学生课程分数类,源代码如下。
要求:
(1)读懂该类的索引类。
(2)运用索引器的重载知识,添加查找张三所有课程的成绩的功能。
01.using System;
02.using System.Collections;
03.//这次我们新建一个类,专门用来存放某个学生某门课程的分数
04.//存放信息:姓名,课程ID,成绩
05.class CourseScore //课程的分数类
06.{
07. private string name;
08. private int courseID;
09. private int score;
10. //构造函数
11. public CourseScore(string name, int courseID, int score)
12. {
13. this.name = name;
14. this.courseID = courseID;
15. this.score = score;
16. }
17. //给私有字段加上属性
18. public string Name
19. {
20. get { return name; }
21. set { name = value; }
22. }
23. public int CourseID
24. {
25. get { return courseID; }
26. set { courseID = value; }
27. }
28. public int Score
29. {
30. get { return score; }
31. set { score = value; }
32. }
33.}
34.//做一个这个类的索引器
35.class CourseScoreIndexer //索引器,用于存取和查询CourseScore类
36.{
37. //ArrayList本身也是个索引器
38. private ArrayList arrCourseScore;
39. //无参构造
40. public CourseScoreIndexer()
41. {
42. //实例化
43. arrCourseScore = new ArrayList();
44. }
45. //索引器
46. //我们希望通过姓名和课程ID可以查找到一个分数
47. //索引器可以使用一个或者多个参数,而且多个参数之间可以是不同的数据类型
48. public int this[string name, int courseID]
49. {
50. get
51. {
52. foreach (CourseScore cs in arrCourseScore)
53. {
54. if (cs.Name == name && cs.CourseID == courseID)
55. {
56. return cs.Score;
57. }
58. }
59. return -1;
60. }
61. set
62. {
63. //name和courseID是索引器的参数,value是一个隐含的值(90就是value)
64. arrCourseScore.Add(new CourseScore(name, courseID, value)); //arr["张三",1]=90
65. }
66. }
67.}
68.class Test
69.{
70. static void Main()
71. {
72. //声明一个索引器
73. CourseScoreIndexer csi = new CourseScoreIndexer();
74. csi["张三", 1] = 90;
75. csi["张三", 2] = 80;
76. csi["张三", 3] = 85;
77. csi["李四", 1] = 80;
78. Console.WriteLine(csi["张三", 2]);
79. }
80.}
...全文
100 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
colorall 2010-11-12
  • 打赏
  • 举报
回复
我怎么没看懂想问什么呢。。。。。。。。。。。。
Teng_s2000 2010-11-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 colorall 的回复:]
我怎么没看懂想问什么呢。。。。。。。。。。。。
[/Quote]
明显是开源嘛
mycroo 2010-11-12
  • 打赏
  • 举报
回复
是不是想让大家帮你做课程设计的?

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧