您的位置:首页 > 其它

关于虚方法(Virtual)的理解

2009-01-06 04:49 197 查看
可以运行一下试试~你就会明白

using System;
using System.Collections.Generic;
using System.Text;

namespace test
{
class Program
{
static void Main(string[] args)
{
FlowB flowB = new FlowB();
flowB.Run(); //复用了Flow的流程并采用了自己的B步骤
Console.ReadLine();
}
}
public class Flow
{
public void A()
{
Console.WriteLine("F A");
}

public virtual void B()
{
Console.WriteLine("F B");
}

public void C()
{
Console.WriteLine("F C");
}

public void Run()
{
A();
B(); //步骤B是扩展点 ,可以由子类决定具体执行什么
C();
}
}

public class FlowA : Flow
{
public override void B()
{
//执行FlowA的B步骤
}
}

public class FlowB : Flow
{
public override void B()
{
Console.WriteLine("FBB");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: