|
发表于 2014-5-5 23:25:47
|
显示全部楼层
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList myList = new ArrayList();
myList.Add(new Student("王同学", 12, '男', "唱歌"));
myList.Add(new Teacher("刘老师", 30, '女', 20));
foreach (Person item in myList)
{
item.SayHello();
}
Console.ReadKey();
}
}
public abstract class Person
{
public Person() { }
public Person(string name, int age, char sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
private string name;
private int age;
private char sex;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public char Sex
{
get { return sex; }
set { sex = value; }
}
public abstract void SayHello();
public virtual void Greet()
{
Console.Write("我叫:{0},我今年:{1}岁了,我是{2}生,", name, age, sex);
}
}
public class Student : Person
{
public Student(string name, int age, char sex, string hobby)
: base(name, age, sex)
{
this.hobby = hobby;
}
private string hobby;
public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
public override void SayHello()
{
base.Greet();
Console.WriteLine("我的爱好是:{0}", hobby);
}
public override void Greet()
{
base.Greet();
Console.WriteLine("我的爱好是:{0}", hobby);
}
}
public class Teacher : Person
{
public Teacher(string nane, int age, char sex, int year)
: base(nane, age, sex)
{
this.year = year;
}
private int year;
public int Year
{
get { return year; }
set { year = value; }
}
public override void SayHello()
{
Console.WriteLine("我叫:{0},我今年:{1}岁了,我是:{2}生,我已经工作:{3}年了", Name, Age, Sex, year);
}
public override void Greet()
{
Console.WriteLine("我叫:{0},我今年:{1}岁了,我是:{2}生,我已经工作:{3}年了", Name, Age, Sex, year);
}
}
}
|
|