using System;

namespace ConsoleApp { class Person { protected int no;//编号 public virtual void display()//输出相关信息 { } public int No { get { return no; } } }

class Student : Person
{
    private int[] scores = new int[5];//5门课成绩
    public override void display()
    {
        int cnt = 0, sum = 0;
        foreach (int i in scores)
        {
            if (i != -1)
            {
                cnt++;
                sum += i;
            }
        }
        Console.WriteLine('{0} {1} {2}', no, 5 - cnt, cnt == 0 ? 0 : (int)Math.Round((double)sum / cnt));
    }
    public int[] Scores { get { return scores; } }
}

class Teacher : Person
{
    private int[] papers = new int[3];//3年的论文数
    public override void display()
    {
        int sum = 0;
        foreach (int i in papers) sum += i;
        Console.WriteLine('{0} {1}', no, sum);
    }
    public int[] Papers { get { return papers; } }
}

class Program
{
    static void Main(string[] args)
    {
        Person[] persons = new Person[10];//人员数组
        int cnt = 0;
        while (true)
        {
            string s = Console.ReadLine();
            if (s == "0") break;
            string[] ss = s.Split(' ');
            int type = int.Parse(ss[0]);
            int no = int.Parse(ss[1]);
            if (type == 1)
            {
                Student student = new Student();
                student.No = no;
                for (int i = 0; i < 5; i++)
                {
                    student.Scores[i] = int.Parse(ss[i + 2]);
                }
                persons[cnt++] = student;
            }
            else
            {
                Teacher teacher = new Teacher();
                teacher.No = no;
                for (int i = 0; i < 3; i++)
                {
                    teacher.Papers[i] = int.Parse(ss[i + 2]);
                }
                persons[cnt++] = teacher;
            }
        }
        for (int i = 0; i < cnt; i++)
        {
            persons[i].display();
        }
    }
}

}

C# 基类框架:Person、Student、Teacher 类实现及多态应用

原文地址: https://www.cveoy.top/t/topic/n5fd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录