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


    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();
        }
    }
    

      

  • 相关阅读:
    php基本语法之逻辑运算符
    HTML5实现端访问时禁止放大和缩小网页
    javascript simple MVC
    GIS开发离线地图应用-初识gis
    百度应用开发--日期大写转换
    ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录
    ThinkPHP与EasyUI整合之二(datagrid):删除多条记录
    面向对象----类和对象
    Java 基本语法----数组
    Java 基本语法----流程控制
  • 原文地址:https://www.cnblogs.com/i124q29/p/3857573.html
Copyright © 2020-2023  润新知