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(); }
}