本帖最后由 9881008杜鹏 于 2011-11-27 00:28 编辑
abstract class Person
{
public Person(string vocation, string name, int age)
{
this.vocation = vocation;
this.name = name;
this.age = age;
}
protected string vocation;
protected string name;
protected int age;
public string Vocation { set { vocation = value; } get { return vocation; } }
public string Name { set; get; }
public int Age { set; get; }
public abstract void SayHello();
public abstract void StandUp();
}
class Teacher: Person
{
public Teacher(string vocation, string name, int age, int salary):base(vocation ,name,age)//在被继承的构造函数中(base中没有类型(如string int))
{
////this.vocation = base.vocation;
////this.Name = base.name;
//this.age = base.age;
this.salary = salary;
Console.WriteLine("职业类型:{0} \n 姓名:{1} \n 年龄:{2} \n 工资:{3}RMB(月薪)。",vocation ,name ,age ,salary );
}
private int salary;
public int Salary { set; get; }
public override void SayHello()
{
Console.WriteLine("Good morning every one !");
}
public override void StandUp()
{
Console.WriteLine(" Please sit down !");
}
}
class Student: Person
{
public Student(string vocation, string name, int age, int grade)
: base(vocation, name, age)
{
this.grade = grade;
Console.WriteLine("职业类型:{0} \n 姓名:{1} \n 年龄:{2} \n 成绩:{3}", vocation, name, age, grade );
}
private int grade ;
public int Grade{get;set;}
public override void SayHello()
{
Console.WriteLine("Good morning teacher ! ");
}
public override void StandUp()
{
Console.WriteLine(" Please stand up ! ");
}
}
static void Main(string[] args)
{
Person [] person = new Person[2];
person[0] = new Student("学生","Tom",12,399);
person[1] = new Teacher ("老师", "杨中科", 12, 99999);
person[0].StandUp();
person[1].SayHello();
person[0].SayHello();
person[1].StandUp();
for (int i = 0; i < person.Length; i++)
{
Console.WriteLine(person.SayHello(), person.StandUp());//最匹配的重载方法具有一些无效的参数
}
Console.ReadKey();
怎么回事呢?
|