首页 / 软件开发 / C# / C#语法练习(10): 类[二] - 继承、覆盖、多态、隐藏
C#语法练习(10): 类[二] - 继承、覆盖、多态、隐藏2011-09-22 博客园 万一继承: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();
}
}