通过使用virtual和override修饰符,将基类的引用提升到派生类;
namespace 虚方法
{
internal class Program
{
class Father
{
virtual public void A()
{
Console.WriteLine("这是father");
}
class son : Father
{
virtual public void A()
{
Console.WriteLine("这是son");
}
}
static void Main(string[] args)
{
son son = new son();
son.A();
Father father = (Father)son;
son.A();
Console.ReadLine();
}
}
}
}