
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class TheFather
{
public void BaseMethod1()
{
Console.WriteLine("吃了吗?");
BaseMethod2();
}
public virtual void BaseMethod2()
{
}
}
public class Son1 : TheFather
{
public override void BaseMethod2()
{
Console.WriteLine("吃了");
}
}
public class Son2 : TheFather
{
public override void BaseMethod2()
{
Console.WriteLine("还没呢");
}
}
public class Client
{
public static void Main()
{
TheFather obj = new Son1();
obj.BaseMethod1();
obj = new Son2();
obj.BaseMethod1();
Console.Read();
}
}
}
本文通过一个简单的 C# 示例介绍了多态的概念及其使用方式。示例中定义了一个基类 TheFather 和两个派生类 Son1 和 Son2,演示了如何通过基类引用调用派生类的不同实现。

被折叠的 条评论
为什么被折叠?



