using System;
class Parent
{
public void Msg() { Console.WriteLine("Parent"); }
}
class Child : Parent { }
class Program
{
static void Main()
{
Parent ObjParent = new Parent();
Child ObjChild = new Child();
ObjParent.Msg(); //Parent
ObjChild.Msg(); //Parent
Console.ReadKey();
}
}
覆蓋:
using System;
class Parent
{
public virtual void Msg() { Console.WriteLine("Parent"); }
}
class Child : Parent
{
public override void Msg() { Console.WriteLine("Child"); }
}
class Program
{
static void Main()
{
Parent ObjParent = new Parent();
Child ObjChild = new Child();
ObjParent.Msg(); //Parent
ObjChild.Msg(); //Child
Console.ReadKey();
}
}