定义抽象类Person、派生类Student和类Teacher(C#实现)

设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。
数据成员定义:
Person [ID,姓名,生日]
Student [专业,成绩]
Teacher [职称,工资]
带参构造方法分别为:
Person(int id,String name, int bir)
Student(int id,String name, int bir, String major,double score)
Teacher(int id,String name, int bir, String title, double salary)
toString方法(Eclipse自动生成)

输入格式:

第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。

输出格式:

按行输出具体对象的信息。

输入样例:

在这里给出一组输入。例如:

5
0
10101 Peter 20010121 Computer 87.5
1
20110014 Crystal 19900103 AI 7000
0
10102 Rose 20010715 E-Commerce 90
1
20120019 David 19781218 Prof 12000
0
10103 Semon 20000405 Computer 88

输出样例:

在这里给出相应的输出。例如:

Student [id=10101, name=Peter, bir=20010121, major=Computer, score=87.5]
Teacher [id=20110014, name=Crystal, bir=19900103, title=AI, salary=7000.0]
Student [id=10102, name=Rose, bir=20010715, major=E-Commerce, score=90.0]
Teacher [id=20120019, name=David, bir=19781218, title=Prof, salary=12000.0]
Student [id=10103, name=Semon, bir=20000405, major=Computer, score=88.0]


解答:要注意是全部一次性输入,并且要注意题目最后的格式要保留一位小数

using System;

namespace _7_13_定义抽象类Person_派生类Student和类Teacher
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int i =int.Parse(Console.ReadLine());
            Person[] people = new Person[i];
            for(int a = 0; a < i; a++)
            {
                int b = int.Parse(Console.ReadLine());
                if(b== 0)
                {
                    string arr=Console.ReadLine();
                    string[] arr1 = arr.Split(' ');
                    int id=int.Parse(arr1[0]);
                    int bir=int.Parse(arr1[2]);
                    double score=double.Parse(arr1[4]);
                    people[a] = new Student(id, arr1[1], bir, arr1[3], score);
                }
                else if (b == 1)
                {
                    string arr = Console.ReadLine();
                    string[] arr1 = arr.Split(' ');
                    int id = int.Parse(arr1[0]);
                    int bir = int.Parse(arr1[2]);
                    double salary = double.Parse(arr1[4]);
                    people[a] = new Teacher(id, arr1[1], bir, arr1[3], salary);
                }
                else
                {
                    break;
                }
                }
            foreach(var item in people)
            {
                Console.WriteLine(item.ToString());
                
            }
            }
        }
    }

    public abstract class Person
    {
        private int _id;
        private string _name;
        private int _bir;
        public int Id { get; set; }
        public string Name { get; set; }
        public int Bir { get; set; }
        public Person(int id, string name, int bir)
        {
            this.Id = id;
            this.Name = name;
            this.Bir = bir;
        }
    }
    public class Student : Person
    {
        private string _major;
        private double _score;
        public string Major { get; set; }
        public double Score { get;set; }
        public Student(int id,string name,int bir,string major,double score) : base(id, name, bir)
        {
            this.Id=id;
            this.Name = name;
            this.Bir=bir;
            this.Major = major;
            this.Score=score;
        }
        public override string ToString()
        {
            return "Student [id=" + this.Id + ", name=" + this.Name + ", bir=" + this.Bir + ", major=" + this.Major + ", score=" + this.Score.ToString("#0.0") + "]";
        }
    } 
    public class Teacher : Person
    {
        private string _title;
        private double _salary;
        public string Title { get; set; }
        public double Salary { get; set; }
        public Teacher(int id,string name,int bir,string title,double salary) : base(id, name, bir)
        {
            this.Id = id;
            this.Name = name;
            this.Bir = bir;
            this.Title = title;
            this.Salary = salary;
        }  
        public override string ToString()
        {
            return "Teacher [id="+this.Id+", name="+this.Name+", bir="+this.Bir+", title="+this.Title+", salary="+this.Salary.ToString("#0.0") + "]";
        }

    }

猜你喜欢

转载自blog.csdn.net/MONOJP/article/details/127872939