c#定义一个人员类Person,包括数据成员

在c#中 设计一个控制台应用程序,定义一个人员类Person,包括数据成员:姓名、编号、性别及用于输入输出的虚方法成员。在此基础上派生出学生类Student(增加成绩)和教师类Teacher(增加教龄),并实现对学生和教师信息的输入/输出。

第1个回答  推荐于2017-05-25
public class Person
{ public Person() { }
public Person(string _name, int _number, int _age)
{
this.Name = _name;
this.Number = _number;
this.Age = _age;
}
string name; public string Name
{
get { return name; }
set { name = value; }
}
int number; public int Number
{
get { return number; }
set { number = value; }
}
int age; public int Age
{
get { return age; }
set
{
if (value > 100 || value < 0)
age = 18;
else
age = value;
}
}
public virtual void SayHi()
{
Console.WriteLine("大家好!我叫{0},今年{1}岁,我的编号是{2}",
Name, Age, Number);
} }
public class Student : Person
{
public Student() { }
public Student(string _name, int _number, int _age, double _score)
: base(_name, _number, _age)
{
this.Score = _score;
}
double score; public double Score
{
get { return score; }
set { score = value; }
}
public override void SayHi()
{
Console.WriteLine("大家好!我是{0}同学,今年{1}岁,我的编号{2},今天的成绩为{3}",
base.Name, base.Age, base.Number, this.Score);
}
}
public class Teacher : Person
{
public Teacher() { }
public Teacher(string _name, int _number, int _age, int _yearsOfService)
: base(_name, _number, _age)
{
this.YearsOfService = _yearsOfService;
}
int yearsOfService; public int YearsOfService
{
get { return yearsOfService; }
set { yearsOfService = value; }
}
public override void SayHi()
{
Console.WriteLine("大家好!我是{0}老师,今年{1}岁,我的编号{2},我今年工作{3}年了",
base.Name, base.Age, base.Number, this.YearsOfService);
}
}
public class Program
{
static void Main(string[] args)
{
Person student = new Student("毛蛋", 17, 1001, 87.00);
student.SayHi();
Person teacher = new Teacher("型男",28,1015,4);
teacher.SayHi(); }
}
相似回答