Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器 软件资源

软件开发小程序制作系统集成与运维空间租用硬件开发视频监控技术咨询与支持——联系电话:0311-88999002/88999003

首页 / 软件开发 / 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();
}
}