class Program
{
static void Main(string[] args)
{
Person p = new Student();
Console.WriteLine(p.GetType());
p.SayHi();
Console.ReadKey();
}
class Person
{
public void SayHi()
{
Console.WriteLine("我是人类");
}
}
class Student : Person
{
public new void SayHi()
{
Console.WriteLine("我是学生");
}
}
}
如代码,p 这个对象通过p.getType()可知它的类型是Student类
既然是Student类
那么调用它的SayHi方法就应该调用Student类里的SayHi方法啊
可是实际上却调用了Person类的SayHi方法
结果应该所有人都知道,但是为什么呢
真的很不理解为什么,有没有比较懂原理的解释一下~