编写出一个通用的人员类(Person),该类具有姓名、年龄、性别等属性,然后通过对Pers
编写出一个通用的人员类(Person),该类具有姓名、年龄、性别等属性,然后通过对Person类的继承得到一个学生类,该类最多能够存放学生5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出3种形式,最后编程对学生类的功能进行验证。
该类‘最多’能够存放学生5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出3种形式,最后编程对学生类的功能进行验证。
类编写出来旅哗陆了,测拆顷试的方法请自己动手写下
///
///Person类
///
using System;
using System.Collections.Generic;
using System.Text;
namespace Person
{
public class Person
{
private string name;
private int age;
private string address;
private char sex;
private string phone;
private string mobile;
public Person() { }
public Person(string name)
{
this.name = name;
}
public Person(string name, int age, char sex):this(name)
{
this.age = age;
this.sex = sex;
}
public Person(string name, int age, char sex, string address)
: this(name, age, sex)
{
this.address = address;
}
public string Mobile
{
get { return mobile; }
set { mobile = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
public char Sex
{
get { return sex; }
set { sex = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
}
///
///课程类芦卖
///
using System;
using System.Collections.Generic;
using System.Text;
namespace Person
{
public class Course
{
private string courseName;
private int score;
public Course() { }
public Course(string name, int score)
{
this.courseName = name;
this.score = score;
}
public int Score
{
get { return score; }
set { score = value; }
}
public string CourseName
{
get { return courseName; }
set { courseName = value; }
}
}
}
///
///学生类,继承自Person
///
using System;
using System.Collections.Generic;
using System.Text;
namespace Person
{
public class Student:Person
{
private Course[] courses;
public Student()
: base()
{
this.courses = new Course[5];
}
public Student(string name)
: base(name)
{
this.courses = new Course[5];
}
public Student(string name, int age, char sex)
: base(name, age, sex)
{
this.courses = new Course[5];
}
public Student(string name, int age, char sex, string address, Course[] courses)
: base(name, age, sex, address)
{
this.courses = courses;
}
public Course[] Courses
{
get { return courses; }
set { courses = value; }
}
//计算平均成绩的方法
public double AverageScore()
{
int sum = 0;
int courseScore = 0;
int courseCount = 0;
for (int i = 0; i < this.courses.Length; i++)
{
if (courses[i].CourseName!=null || string.IsNullOrEmpty(courses[i].CourseName))
{
courseScore = courses[i].Score;
courseCount++;
sum += courseScore;
}
}
return sum * 1.0 / courseCount;
}
}
}