用C#编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等属性。然后对Person 类的继承得到一个学生类(Student),Student类能够存放5门课的成绩,并能求出平均成绩。最后在Main函数中对Student类的功能进行验证

解决方案 »

  1.   


        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Sex { get; set; } 
        }
        public class Student : Person
        {
            /// <summary>
            /// 语文分数
            /// </summary>
            public int ChineseScore { get; set; }
            /// <summary>
            ///  数学分数
            /// </summary>
            public int MathScore { get; set; }
            /// <summary>
            ///  英语分数
            /// </summary>
            public int EnglishScore { get; set; }
            /// <summary>
            ///  物理分数
            /// </summary>
            public int PhysicsScore { get; set; }
            /// <summary>
            ///  化学分数
            /// </summary>
            public int ChemistryScore { get; set; }
        }        static void Main(string[] args)
            {
                Student li = new Student();
                li.Name = "李四";
                li.Age = 18;
                li.Sex = "男";
                li.ChineseScore = 91;
                li.MathScore = 85;
                li.EnglishScore = 48;
                li.PhysicsScore = 95;
                li.ChemistryScore = 85;
                Console.WriteLine(String.Format("学生姓名:{0},性别:{1},年龄:{2}",li.Name,li.Sex,li.Age));
                Console.WriteLine(String.Format("语文成绩:{0},数学成绩:{1},英语成绩:{2},物理成绩:{3},化学成绩:{4}", li.ChineseScore, li.MathScore, li.EnglishScore, li.PhysicsScore, li.ChemistryScore));
                double avgScore = (li.ChineseScore + li.MathScore + li.EnglishScore + li.PhysicsScore + li.ChemistryScore) / 5.0;
                Console.WriteLine(String.Format("学生姓名:{0},五科平均分:{1}", li.Name, avgScore));
                Console.ReadKey();
            }
      

  2.   

    太谢谢了,我刚刚自己也实现了一个。
    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace task4{    class Person    {        public Person(string name, string age, string sex)        {            this.Name = name;            this.Age = age;            this.Sex = sex;        }        public String Name { get; set; }        public string Age { get; set; }        public String Sex { get; set; }    }        class Student : Person    {        public Student(string name, string age, string sex)        : base(name, age, sex)        {                       Console.Write("{0}的语文成绩为:", Name);            int str4 = int.Parse(Console.ReadLine());            Grade = str4;            Console.Write("{0}的数学成绩为:", Name);            int str = int.Parse(Console.ReadLine());            Grade1 = str;            Console.Write("{0}的英语成绩为:", Name);            int str1 = int.Parse(Console.ReadLine());            Grade2 = str1;            Console.Write("{0}的物理成绩为:", Name);            int str2 = int.Parse(Console.ReadLine());            Grade3 = str2;            Console.Write("{0}的化学成绩为:", Name);            int str3 = int.Parse(Console.ReadLine());            Grade4 = str3;        }        public int Grade { get; set; }        public int Grade1 { get; set; }        public int Grade2 { get; set; }        public int Grade3 { get; set; }        public int Grade4 { get; set; }        public void Avg()        {            int sum = 0;            int avg = 0;            sum = Grade + Grade1 + Grade2 + Grade3 + Grade4;            avg = sum / 5;            Console.WriteLine("他的平均成绩为{0}", avg);        }    }    class Program    {        static void Main(string[] args)        {            Console.Write("学生姓名:");            string Name = Console.ReadLine();            Console.Write("学生年龄:");            string Age = Console.ReadLine();            Console.Write("学生性别:");            string Sex = Console.ReadLine();            Student student1 = new Student(Name, Age, Sex);            student1.Avg();        }    }}