C#程序设计实验报告面向对象程序设计(实验1第一题)附源码

课程名称 C#程序设计

实验名称 实验一

壹、第一题

一、实验题目

定义一个人类(Person),包括属性:姓名(name)、性别(sex)、年龄(age);包括方法:display(),输出个人信息,吃饭(eat)、睡觉(sleep)、工作(work)。

二、实验要求

(1) 定义带参构造函数,初始化属性值
(2) 定义析构函数,输出Person对象的信息
(3) 定义默认构造函数,实现构造函数重载,测试构造函数和析构函数的构造和析构顺序
(4)根据人类,派生一个学生类(Student),增加属性:学校(school)、学号(number)、平均成绩、语数外各科成绩(scores,一个数组),为该数组实现索引器,
(5)定义Student的带参构造函数,实现学生属性的初始化,部分属性调用父类构造函数来实现
(6)定义Student的非静态函数total和average求学生总成绩和平均成绩
(7)在Student中重写工作方法(学生的工作是学习),重写display方法,输出学生信息
(8)在主函数中定义学生数组或者集合,并对集合初始化,实现对学生数组的排序(按学号、按姓名、按平均成绩排序)
(9)重载加法运算符,两个学生对象相加等价于学生平均成绩相加
(10)根据人类,派生一个工人类(Worker),增加属性:单位,工龄;重写工作方法(工人的工作是……自己想想吧)。
(11)根据学生类,派生一个学生干部类(StudentLeading),增加属性:职务(job);增加方法:开会(meeting)。
(12) 编写主类分别对上述3类具体人物进行测试。

三、实验代码以及执行结果

1、Person类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验一
{
    class Person
    {
        protected string name;
        protected string sex;
        protected int age;

        ~Person()
        {
            Console.WriteLine("这是Person类的析构函数");
            
        }
        public Person(string name,string sex,int age)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
        public Person()
        {
            Console.WriteLine("这是Perosn类的默认构造函数");
            Console.WriteLine();
        }
        public void display()
        {
            Console.WriteLine("姓名:{0} 性别:{1} 年龄:{2}",name,sex,age);
        }
        public void eat()
        {
            Console.WriteLine(name + "吃饭");
        }
        public void sleep()
        {
            Console.WriteLine(name + "睡觉");
        }
        public void work()
        {
            Console.WriteLine(name + "工作");
        }
    }
2、Student类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验一
{
    class Student : Person
    {
        private string school;  //学校
        private string number;  //学号
        private int avgScore;   //平均成绩
        private int[] scores;   //语数外各科成绩数组

        public Student(string name,string sex,int age,string school, string number,int[] scoress) : base(name,sex,age)
        {
            this.school = school;
            this.number = number;
            scores = scoress;
            avgScore = average();
        }
        public Student(int avgScore)
        {
            this.avgScore = avgScore;
        }
        public int total()//求总成绩
        {
            int sum = 0;
            for (int i = 0; i < scores.Length; i++)
            {
                sum += scores[i];
            }
            return sum;
        }

        public int average()//求平均成绩
        {
            return total()/ scores.Length;
        }

        //成绩数组索引器
        public int this[int nindex]
        {
            get { return scores[nindex]; }
            set { scores[nindex] = value; }
        }

        public void work()
        {
            Console.WriteLine(name + "学习");
        }

        public void display()
        {
            base.display();
            Console.WriteLine("学校:{0} 学号:{1} 平均成绩:{2} 语数外各科成绩:{3} {4} {5}",school,number,avgScore,scores[0],scores[1],scores[2]);
            Console.WriteLine();
        }

        public string getNumber()
        {
            return number;
        }
        public string getName()
        {
            return name;
        }
        public int getAvgScore()    //获取平均成绩
        {
            return avgScore;
        }
        //重载加法运算符   两个学生对象相加相当于其平均成绩相加
        public static Student operator +(Student student1, Student student2)
        {
            return new Student(student1.getAvgScore() + student2.getAvgScore());
        }

    }
}
3、StudentLeading类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验一
{
    class StudentLeading : Person
    {
        private string job; //职务
        public StudentLeading(string name, string sex, int age, string job) : base(name, sex, age)
        {
            this.job = job;
        }
        public void meeting()
        {
            Console.WriteLine(name + "开会");
        }
        public void display()
        {
            base.display();
            Console.WriteLine("职务:{0}",job);
            Console.WriteLine();
        }
    }
}
4、Worker类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实验一
{
    class Worker : Person
    {
        private string company;     //单位
        private int workingyears;   //工龄
        public Worker(string name, string sex, int age, string company,int workingyears) : base(name, sex, age)
        {
            this.company = company;
            this.workingyears = workingyears;
        }
        public void work()
        {
            Console.WriteLine(name, "  偷电动车电瓶");
        }
        public void display()
        {
            base.display();
            Console.WriteLine("单位:{0} 工龄:{1}", company, workingyears);
            Console.WriteLine();
        }
    }
}
5、主类
using System;
using System.Collections.Generic;

namespace 实验一
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Student student1 = new Student("张三", "男", 20, "南华大学", "20180440101", new int[] { 80, 80, 80 });
            Student student2 = new Student("李四", "男", 19, "南华大学", "20190440101", new int[] { 90, 90, 90 });
            Student student3 = new Student("王五", "男", 18, "南华大学", "20200440101", new int[] { 95, 95, 95 });
            List<Student> students = new List<Student> { student1, student2, student3 };
            student1[0] = 85; student1[1] = 85; student1[2] = 85; //用数组索引器对张三的成绩进行修改

            //先按学号排序:
            students.Sort(new NumberComparer());
            Console.WriteLine("按学号排序之后的学生信息:");
            for(int i = 0;i < students.Count; i++)
            {
                students[i].display();
                
            }

            //再按姓名排序:
            students.Sort(new NameComparer());
            Console.WriteLine("按姓名排序之后的学生信息:");
            for (int i = 0; i < students.Count; i++)
            {
                students[i].display();

            }

            //最后按平均成绩排序:
            students.Sort(new AvgScoreComparer());
            Console.WriteLine("按平均成绩排序之后的学生信息:");
            for (int i = 0; i < students.Count; i++)
            {
                students[i].display();
            }

            Student studentadd = student1 + student2;
            Console.WriteLine(student1.getName() + "与" + student2.getName() +"的平均成绩之和:" + studentadd.getAvgScore());
            Console.WriteLine();

            Worker worker = new Worker("窃格瓦拉", "男", 37, "监狱",18);
            StudentLeading studentleading = new StudentLeading("刘志勇", "男", 19, "体育部副部长");
            worker.display();
            studentleading.display();

            student1.work();
            studentleading.sleep();
            studentleading.eat();       
            studentleading.meeting();
        }
    }
    // 学号比较器
    class NumberComparer : IComparer<Student>
    {
        //实现升序
        public int Compare(Student x, Student y)
        {
            return (x.getNumber().CompareTo(y.getNumber()));
        }
    }
    // 姓名比较器
    class NameComparer : IComparer<Student>
    {
        //实现升序
        public int Compare(Student x, Student y)
        {
            return (x.getName().CompareTo(y.getName()));
        }
    }
    // 平均成绩比较器
    class AvgScoreComparer : IComparer<Student>
    {
        //实现升序
        public int Compare(Student x, Student y)
        {
            return (x.getAvgScore().CompareTo(y.getAvgScore()));
        }
    }
}
6、执行结果:

在这里插入图片描述

四、实验总结

通过这次实验,我学会了如何构造数组索引器

//成绩数组索引器
        public int this[int nindex]
        {
            get { return scores[nindex]; }
            set { scores[nindex] = value; }
        }
	student1[0] = 85; student1[1] = 85; student1[2] = 85; //用数组索引器对张三的成绩进行修改

这样就可以直接通过数组下标来修改学生的成绩。
我还学会了如何构造集合的比较器

// 平均成绩比较器
    class AvgScoreComparer : IComparer<Student>
    {
        //实现升序
        public int Compare(Student x, Student y)
        {
            return (x.getAvgScore().CompareTo(y.getAvgScore()));
        }
}

//最后按平均成绩排序:
            students.Sort(new AvgScoreComparer());
            Console.WriteLine("按平均成绩排序之后的学生信息:");
            for (int i = 0; i < students.Count; i++)
            {
                students[i].display();
}

这样就可以对学生集合根据平均成绩实现升序排序。
我还学会了如何重载加法运算符,使得两个学生对象相加等价于学生平均成绩相加

//重载加法运算符   两个学生对象相加相当于其平均成绩相加
        public static Student operator +(Student student1, Student student2)
        {
            return new Student(student1.getAvgScore() + student2.getAvgScore());
        }
Student studentadd = student1 + student2;
            Console.WriteLine(student1.getName() + "与" + student2.getName() +"的平均成绩之和:" + studentadd.getAvgScore());
            Console.WriteLine();

只不过这里返回的学生对象只有平均成绩属性,需要另外构造一个只带平均成绩参数的构造函数。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别卷了,球球了。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值