• 构造器练习,对着教学视频敲的,熟悉一下吧


    using System;
    using System.Collections; //要用ArrayList要引用这个
    class CourseScore {  //普通的属性用法
        private string name;
        private int courseID;
        private int score;
        public CourseScore(string name, int courseID, int score) {
            this.name = name;
            this.courseID = courseID;
            this.score = score;
        }
        public string Name {
            get { return name; }
            set { name = value; }
        }
        public int CourseID {
            get { return courseID; }
            set { courseID = value; }
        }
        public int Score {
            get { return score; }
            set { score = value; }
        }
    }
    class CourseScoreIndexer {  //构造器
        private ArrayList arrCourseScore;
        public CourseScoreIndexer() {
            arrCourseScore = new ArrayList();
        }
        public int this[string name, int courseID] {  //构造器只能用this
            get {
                foreach (CourseScore cs in arrCourseScore) {
                    if (cs.Name == name && cs.CourseID == courseID) {
                        return cs.Score;
                    }
                }
                return -1;
            }
            set {
                arrCourseScore.Add(new CourseScore(name, courseID, value));  //hashtable用.Add
            }
        }
        public ArrayList this[string name] {
            get {
                ArrayList tempArr = new ArrayList();
                foreach (CourseScore cs in arrCourseScore) {
                    if (cs.Name == name) {
                        tempArr.Add(cs);
                    }
                }
                return tempArr;
            }
        }
    }
    class Test {
        static void Main() {
            CourseScoreIndexer csi = new CourseScoreIndexer();
            csi["张三", 1] = 90;
            csi["张三", 2] = 80;
            csi["李四", 1] = 70;
            Console.WriteLine("张三的第2门课程分数为:" + csi["张三", 2]);
            ArrayList tempArr = csi["张三"];
            foreach (CourseScore cs in tempArr) {
                Console.WriteLine("姓名:" + cs.Name + "  课程编号:" + cs.CourseID + "  分数:" + cs.Score);
            }
            Console.ReadKey();
        }
    }
    

      

  • 相关阅读:
    飞鸽传书中文源码
    nohup命令参考
    Linux平台编程新手入门 C语言中的移位操作
    小技巧:让linux程序在后台运行
    2440之中断管理
    linux终端中输出彩色字体(C/SHELL)
    C语言标准中的逻辑位移和算术位移
    SQL2005利用ROW_NUMER实现分页的两种常用方式
    不用现有方法,把string转换成int型[C#]
    C# 如何生成一个时间戳
  • 原文地址:https://www.cnblogs.com/i124q29/p/3857573.html
Copyright © 2020-2023  润新知