本帖最后由 9881008杜鹏 于 2011-11-27 15:31 编辑
abstract class Person
{
public Person(string name, string address, string holiday)
{
this.name = name;
this.address = address;
this.holiday = holiday;
}
protected string name;
protected string address;
protected string holiday;
public string Name {set;get;}
public string Address { set; get; }
public string Holiday { set; get; }
public abstract void SayHello();
public void Hello()
{
Console.WriteLine("Hello everyone !");
}
public virtual void HappyHoliday()
{
Console.WriteLine("这是我们的传统节日,这一天...");
}
}
class ChinesePerson: Person
{
public ChinesePerson(string name, string address, string holiday)
: base(name, address, holiday)
{
Console.WriteLine("我是来至:{0} 的 {1},我们的传统节日是:{2}",address,name,holiday);
}
public override void SayHello() //重写父类的抽象方法
{
Console.WriteLine("大家好,我是来自中国的萧逸,欢迎指教!");
}
public new void Hello() //重写(覆盖)父类的Hello方法New:隐藏。
{
Console.WriteLine("{0}是我们的传统节日,每年的这段时间是世界人将面临一次大迁徙!\n 他们不远千里奔袭一个地方:家!",holiday );
}
public override void HappyHoliday() //重写虚方法
{
Console.WriteLine("{0}祝大家{1}快乐喔,O(∩_∩)O哈哈~",name ,holiday );
}
}
static void Main(string[] args)
{
Person chineseperson = new ChinesePerson("萧逸","中国","春节");
chineseperson.Hello(); chineseperson.SayHello();
ChinesePerson chinese = new ChinesePerson("萧逸", "中国", "春节");
chinese.Hello();
chineseperson.HappyHoliday();
Console.ReadKey();
}
为什么chineseperson.Hello(); 不能调用在子类中的Hello()方法??? |