C# Deldegate的使用
Delegate类简介
------------------------
命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
委托(Delegate)类是一种数据结构,通过它可引用静态方法或引用类实例及该类的实例方法。
以往的界面编程中我们应该都接触过各种类型的事件驱动(event driven)的处理模式,
在这种模式里,我们定义相应事件触发的函数。
例如:
Button1 的 Click事件,我们可以编写Button1_Click 或 Btn1Clicked等函数来做相应的驱动处理。
而事件与驱动函数的对应关系就是通过委托(Delegate)类来关联的。
其实委托(Delegate)类这种数据结构有些类似于之前C/C++中的函数指针。
Delegate类一个简单应用
------------------------
1.定义一个Delegate函数数据结构
2.定义Delegate将引用的静态方法或引用类实例及该类的实例方法
3.Delegate数据变量指向实例方法
4.通过Delegate数据变量执行实例方法
- A very basic example (TestClass.cs):
- using System;
- namespace MySample
- {
- class TestClass
- {
- //1.定义一个Delegate函数数据结构
- public delegate void GoDelegate();
- [STAThread]
- static void Main(string[] args)
- {
- //3.Delegate数据变量指向实例方法
- GoDelegate goDelegate = new GoDelegate( MyDelegateFunc);
- //4.通过Delegate数据变量执行实例方法
- goDelegate();
- return;
- }
- //2.定义Delegate将引用的静态方法或引用类实例及该类的实例方法
- public static void MyDelegateFunc()
- {
- Console.WriteLine("delegate function...");
- }
- }
- }
- 编译执行结果:
- # TestClass.exe
- delegate function...
使用Delegate类和Override实现多态的比较
-----------------------------------------------
1.使用Delegate类的时候,下面的例子可以很清楚的说明。
1.1 首先定义一个动物基类(MyAnimalDelegateClass), 基类中有显示属性的(ShowAnimalType)的public方法。
并且在ShowAnimalType方法中调用Delegate引用的实例方法
1.2 定义狮子(LionDelegateClass)和马(HorseDelegateClass)两个子类。Delegate与各自的实例方法绑定
实现不同的属性显示(ShowAnimalType)方法。
- ////Delegate example (TestClass.cs):
- using System;
- namespace MySample
- {
- class TestClass
- {
- [STAThread]
- static void Main(string[] args)
- {
- //狮子(LionDelegateClass)的属性显示(ShowAnimalType)方法调用
- LionDelegateClass lionDelegate = new LionDelegateClass();
- lionDelegate.ShowAnimalType("MySample");
- //马(HorseDelegateClass)的属性显示(ShowAnimalType)方法调用
- HorseDelegateClass horseDelegate = new HorseDelegateClass();
- horseDelegate.ShowAnimalType("MySample");
- }
- }
- //动物基类(MyAnimalDelegateClass)
- public class MyAnimalDelegateClass
- {
- //Delegate数据结构定义
- public delegate void DelegateFunction(string strFuncName);
- private DelegateFunction m_delegateFunction = null;
- //Delegate类型的属性
- public DelegateFunction delegateFunction
- {
- get
- {
- return m_delegateFunction;
- }
- set
- {
- m_delegateFunction = value;
- }
- }
- //属性显示(ShowAnimalType)方法
- public void ShowAnimalType(string strFuncName)
- {
- if (delegateFunction != null)
- {
- object[] args = {strFuncName};
- //调用Delegate引用的实例方法
- delegateFunction.DynamicInvoke(args);
- }
- }
- }
- //狮子(LionDelegateClass)
- public class LionDelegateClass:MyAnimalDelegateClass
- {
- public LionDelegateClass()
- {
- this.delegateFunction = new DelegateFunction(subFunction1);
- }
- //狮子(LionDelegateClass)实例方法的实装
- private void subFunction1(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a lion....", strFuncName));
- }
- }
- //马(HorseDelegateClass)
- public class HorseDelegateClass:MyAnimalDelegateClass
- {
- public HorseDelegateClass()
- {
- this.delegateFunction = new DelegateFunction(subFunction2);
- }
- //马(HorseDelegateClass)实例方法的实装
- private void subFunction2(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a horse....", strFuncName));
- }
- }
- }
- 编译执行结果:
- # TestClass.exe
- [MySample]This is a lion....
- [MySample]This is a horse....
2.使用Override实装的时候,参考下面的例子。
1.1 首先定义一个动物基类(AbstractAnimalNoDelegateClass), 基类中有显示属性的(ShowAnimalType)的public方法。
并且在ShowAnimalType方法中调用抽象方法(NoDelegateFunction)
1.2 定义狮子(LionNoDelegateClass)和马(HorseNoDelegateClass)两个子类。
子类中实装抽象方法(NoDelegateFunction)
实现不同的属性显示(ShowAnimalType)方法。
- ////Override example (TestClass.cs):
- using System;
- namespace MySample
- {
- class TestClass
- {
- [STAThread]
- static void Main(string[] args)
- {
- //狮子(LionNoDelegateClass )的属性显示(ShowAnimalType)方法调用
- LionNoDelegateClass lionNoDelegate = new LionNoDelegateClass();
- lionNoDelegate.ShowAnimalType("MySample");
- //马(HorseNoDelegateClass )的属性显示(ShowAnimalType)方法调用
- HorseNoDelegateClass horseNoDelegate = new HorseNoDelegateClass();
- horseNoDelegate.ShowAnimalType("MySample");
- }
- }
- //动物基类(AbstractAnimalNoDelegateClass)
- public abstract class AbstractAnimalNoDelegateClass
- {
- public void ShowAnimalType(string strFuncName)
- {
- //抽象方法(NoDelegateFunction)调用
- NoDelegateFunction(strFuncName);
- }
- //在基类中定义抽象方法(NoDelegateFunction)
- protected abstract void NoDelegateFunction(string strFuncName);
- }
- //狮子(LionNoDelegateClass )
- public class LionNoDelegateClass:AbstractAnimalNoDelegateClass
- {
- // 子类中实装抽象方法(NoDelegateFunction)
- protected override void NoDelegateFunction(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a lion....", strFuncName));
- }
- }
- //马(HorseNoDelegateClass )
- public class HorseNoDelegateClass:AbstractAnimalNoDelegateClass
- {
- // 子类中实装抽象方法(NoDelegateFunction)
- protected override void NoDelegateFunction(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a horse....", strFuncName));
- }
- }
- }
- 编译执行结果:
- # TestClass.exe
- [MySample]This is a lion....
- [MySample]This is a horse....
3.比较Delegate和Override实装方式
可以看出Delegate实装方式中,相当于定义一个函数指针的成员变量。
通过把实装函数的地址赋给该成员变量,实现同样的方法,处理方式的不同。
而Override方式中,则是在父类中预先定义好接口,通过实装的不同,
来实现同样的方法,处理方式的不同。
Delegate实装方式比较灵活,适合设计不是很完善的场合,便于修改。
Override方式封装性好,相对比较安全。
MulticastDelegate 类的应用
---------------------------------
在C#中,委托(Delegate)类是多路委托,这就说可以同时指向多个处理函数,
并且可以按照委托的先后顺序,执行相应的函数。
如下例:
- using System;
- namespace MySample
- {
- class TestClass
- {
- [STAThread]
- static void Main(string[] args)
- {
- DogDelegateClass dogDelegate = new DogDelegateClass();
- dogDelegate.ShowAnimalType("MySample");
- }
- public class MyAnimalDelegateClass
- {
- public delegate void DelegateFunction(string strFuncName);
- private DelegateFunction m_delegateFunction = null;
- public DelegateFunction delegateFunction
- {
- get
- {
- return m_delegateFunction;
- }
- set
- {
- m_delegateFunction = value;
- }
- }
- public void ShowAnimalType(string strFuncName)
- {
- if (delegateFunction != null)
- {
- object[] args = {strFuncName};
- delegateFunction.DynamicInvoke(args);
- }
- }
- }
- public class DogDelegateClass:MyAnimalDelegateClass
- {
- public DogDelegateClass()
- {
- //多路委托函数 设定
- this.delegateFunction = new DelegateFunction(subFunction31);
- this.delegateFunction += new DelegateFunction(subFunction32);
- }
- //委托函数1
- private void subFunction31(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a dog....", strFuncName));
- }
- //委托函数2
- private void subFunction32(string strFuncName)
- {
- System.Console.WriteLine(
- string.Format("[{0}]This is a nice dog....", strFuncName));
- }
- }
- }
- 编译执行结果:
- # TestClass.exe
- [MySample]This is a dog....
- [MySample]This is a nice dog....
转载请表明出处!
C# Deldegate的使用的更多相关文章
随机推荐
- OPENGL: WHY IS YOUR CODE PRODUCING A BLACK WINDOW?
Introduction One of the most common problems for novice, and sometimes experienced, OpenGL program ...
- SharpDX之Direct2D教程I——简单示例和Color(颜色)
研究Direct2D已经有一段时间了,也写了一个系列的文章 Direct2D ,是基于Windows API Code Pack 1.1.在前文 Direct2D教程VIII——几何(Geometry ...
- 在Android Studio中打开Android Device Monitor时报错的解决方法
在Android Studio中打开Android Device Monitor时报以下错误时(Android-SDK\tools\lib\monitor-x86_64\configuration\1 ...
- jQuery 用each后添加click
mydd = $('.plist'); mydd.each(function(i){ $(this).click(function(){ mydl.eq(i).hide("slow" ...
- Go语言使用Beego的ORM插入Mysql后,时区不一致的解决方案
一.解决方案: 方案一: orm.Debug = false orm.DefaultTimeLoc = time.UTC orm.RegisterDriver("mysql", o ...
- POSTGRESQL 自动登录
以前习惯使用MYSQL命令行登录,但是到POSTGRESQL不能实现,下面总结一下方法: 1.填写需要链接的postgresql语句,一般放在~/.bash_profile,例如: alias log ...
- python2x与3x下使用urlretrieve下载文件
1.python2x下urlretrieve方法: 直接将远程数据下载到本地. urllib.urlretrieve(url[, filename[, reporthook[, data]]]) 参数 ...
- js setTimeout setInterval 第三个参数说明
1.api setTimeout: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout var timeoutID = ...
- OpenERP 7中 openerp-server.conf 的解释
服务器启动配置 – 通用项 程序代码: [选择] # Admin password for creating, restoring and backing up databases admin_pas ...
- Oracle Data Integrator 12c----一致性 CDC(Consistent CDC)
一致性 CDC 中引入了变化集的概念.一个变化集中可以包括多个相互存在关联关系(如主外键引用关系)的表.CDC 在捕获和发布一个变化集中的变化时能够保证数据的一致性.这个练习介绍如何使用能够保证一致性 ...