class Animal
{
public virtual void FoodHabits()
{
Console.WriteLine("动物有各种不同的饮食习惯");
}
}
class Carnivorous : Animal
{
public override void FoodHabits()//重写,重写后还是虚方法
{
Console.WriteLine("肉食动物只吃肉");
}
}
class Herbivorouos : Animal
{
public new void FoodHabits()//覆盖
{
Console.WriteLine("食草动物只吃草");
}
}
class Tiger : Carnivorous
{
public override void FoodHabits()//重写
{
Console.WriteLine("老虎不吃人");
}
}
Animal an = new Animal();
an.FoodHabits();
Carnivorous ca = new Carnivorous();
ca.FoodHabits();
Herbivorouos he = new Herbivorouos();
he.FoodHabits();
Animal an2 = new Carnivorous();//基类的指针可以指向任何一个子类的对象
an2.FoodHabits();
Animal an3 = new Herbivorouos();
an3.FoodHabits();
Animal an4 = new Tiger();
an4.FoodHabits();
为什么Carnivorous ca = new Carnivorous();
ca.FoodHabits();
和Animal an3 = new Herbivorouos();
an3.FoodHabits();
显示的结果不一样