关于虚方法(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");
}
}
}
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");
}
}
}
相关文章推荐
- Android中关于ContentProvider类的getType()方法的理解
- JAVA中关于set()和get()方法的理解及使用
- 面向对象的编程:关于对象的 属性、方法和事件 的理解
- JAVA中关于set()和get()方法的理解及使用
- Java中关于Arrays.asList方法的深入学习与理解
- 关于各种输入输出流的二次理解总结 ,以及输入的三种方法总结。
- 关于各种输入输出流的二次理解总结 ,以及输入的三种方法总结。
- Java中关于Arrays.asList方法的深入学习与理解
- 关于.net Enumerable的一些方法的理解
- JAVA中关于set()和get()方法的理解及使用
- Java编程关于子类重写父类方法问题的理解
- 一篇文章理解所有android关于存储的方法
- 关于"多态"static方法的理解
- JAVA中关于set()和get()方法的理解及使用
- IOS初学,关于内存的使用和释放,以及对其方法的理解
- 关于 app2sd、app2ext 的理解 开启方法
- 关于Java两种同步方法的理解(代码块级同步对象设置)
- 关于iOS中属性变量setter与getter方法的理解
- 关于Character的digit,forDigit,getNumericValue方法的一点理解
- 关于VB方法返回值为值类型时,出现要求对象的理解