namespace Inheritance
{
public abstract class Animal
{
public string type="animal";
public abstract void ShowType();
}
public class Bird : Animal
{
public string type = "Bird";
public override void ShowType()
{
Console.WriteLine("Type is {0}", type);
}
}
public class TestInheritance
{
public static void Main()
{
Animal animal = new Bird();
Console.WriteLine("Type value is {0}", animal.type);
animal.ShowType();
Console.ReadKey();
}
}
}
程序输出的结果是:
Type value is animal
Type is Bird
为什么同样是将Bird类型赋给Animal,调用animal.ShowType()时用的是Bird的ShowType()方法,而调用animal.type的值却又是Animal里的type值?
杨魅力