zoukankan      html  css  js  c++  java
  • 练习

    1.属性和索引练习

    (1) 定义一个Student类,其包含属性:SId(学号), Name(姓名),Score(总分),并重载其构造方法,初始化其属性。

    (2) 定义一个班级类ClassDemo,其包含属性Count(学生人数,只读),List<Student>(学生列表);定义其索引器,按照序号获得学生对象,按照学号和姓名获得学生对象(索引重载)。

    (3) 编写测试类,创建4个学生对象:

    学号

    姓名

    总分

    1

    张三

    375

    2

    李四

    400

    3

    王五

    425

    4

    薛六

    498

    1. 将以上学生添加到班级,输出班级人数,并遍历班级学生的信息;
    2. 使用索引器查找学号是2,姓名是“李四”的总分,并输出到控制台。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication4
    {
        class Student 
        {
            int sid;
    
            public int Sid
            {
                get { return sid; }
                set { sid = value; }
            }
            string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            int score;
    
            public int Score
            {
                get { return score; }
                set { score = value; }
            }
            public Student(int _sid, string _name, int _score) 
            {
                this.sid = _sid;
                this.name = _name;
                this.score = _score;
            }
    
        }
        class ClassDemo 
        {
            int count;
            List<Student> stu;
    
            internal List<Student> Stu
            {
                get { return stu; }
                set { stu = value; }
            }
    
            public int Count
            {
                get { return count; }
                
            }
            public ClassDemo(int count)
            {
                this.count = count;
                stu=new List<Student>(count);
            }
            public Student this[int index] 
            {
                get 
                {
                    if (index > 0 && index < stu.Count)
                    {
                        return stu[index];
                    }
                    else return null;
                }
                set 
                {
                    if (index < 0 || index > stu.Count)
                    {
                        Console.WriteLine("索引无效");
                        return;
                    }
                    else stu[index] = value;
                }
            }
            public Student this[string name, int sid] 
            {
                get
                {
                    foreach (Student item in stu)
                    {
                        if (item.Name == name && item.Sid == sid) 
                        {
                            return item;
                        }
                    }
                    return null;
                }
                set 
                {
                    for (int i = 0; i < stu.Count; i++)
                    {
                        if (stu[i].Name == name && stu[i].Sid == sid)
                        {
                            stu[i] = value;
                        }
                    }
    
                }
            }
        }
       class Program
        {
            static void Main(string[] args)  
            {
    
                Student stu1 = new Student(1, "张三", 375);
                Student stu2 = new Student(2, "李四", 375);
                Student stu3 = new Student(3, "王五", 375);
                Student stu4 = new Student(4, "薛六", 375);
    
                ClassDemo dem = new ClassDemo(4);
                dem.Stu.Add(stu1);
                dem.Stu.Add(stu2);
                dem.Stu.Add(stu3);
                dem.Stu.Add(stu4);
                //并遍历班级学生的信息
                foreach (Student item in dem.Stu)
                {
                    Console.Write(item.Name);
                }
                Console.WriteLine();
                //输出学生人数
                Console.WriteLine(dem.Stu.Count);
    
    
                //使用索引器查找学号是2,姓名是“李四”的总分,并输出到控制台。
                dem[0] = stu1;
                dem[1] = stu2;
                dem[2] = stu3;
                dem[3] = stu4;
                Console.WriteLine(dem["李四",2].Score);
    
                
    
                Console.ReadKey();
    
            }
        }
    }

  • 相关阅读:
    A physically based scene with three.js
    pbr-guide
    Art Pipeline for glTF
    GG5D
    Leetcode 146.LRU缓存机制
    Leetcode 143.重排链表
    Leetcode 142.环形链表II
    Leetcode 141.环形链表
    Leetcode 139.单词拆分
    Leetcode 138.复制带随机指针的链表
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/8330956.html
Copyright © 2011-2022 走看看