callback(2)--使用代理
来源:互联网 发布:js控制更多 编辑:程序博客网 时间:2024/04/30 08:49
针对callback(1)的需求,可以使用代理来解决。
/相对于用接口Callback的方式, //代理可以和接口一样指定方法的定义 //代理可以自动维护一个ArrayList,通过简单的 += 和 -= 就可以实现. //如果Car类中,将该代理类型的成员设为public ,甚至可以不用再写维护的方法
CAR
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestCS{ class Car { #region Properties and variables private int maxSpeed = 500; private string petName; private int currentSpeed; public string PetName { get { return petName; } set { petName = value; } } public int CurrentSpeed { get { return currentSpeed; } set { currentSpeed = value; } } #endregion //声明一个代理,此代理为一个inner class的代理。全名为Car.Exploed public delegate void Exploed(string msg); //声明一个该代理类型的成员。 private Exploed delegateExplodedList; //维护该成员 //是否可以改为属性? public void SetDelegateExplodedList(Exploed clientMethod) { this.delegateExplodedList += clientMethod; } public void SetDelegateExplodedListRemove(Exploed clientMethod) { this.delegateExplodedList -= clientMethod; } public Exploed DelegateExplodedList { set { this.delegateExplodedList += value; } } bool isExploed=false; public void Accelerate(int delta) { if (isExploed) { if (delegateExplodedList != null) { //触发此代理 //帮代理赋参数值 delegateExplodedList("(Car is doing---->Car is Dead!)"); } } else { currentSpeed += delta; Console.WriteLine("Current Speed is {0}", currentSpeed.ToString()); if (maxSpeed< currentSpeed) isExploed = true; } } }}
客户端
using System;using System.Collections;using System.Linq;using System.Text;using System.Collections.Generic;using System.Runtime.Serialization;namespace TestCS{ class Program { //相对于用接口Callback的方式, //代理可以和接口一样指定方法的定义 //代理可以自动维护一个ArrayList,通过简单的 += 和 -= 就可以实现. //如果Car类中,将该代理类型的成员设为public ,甚至可以不用再写维护的方法 public static void Main(string[] args) { Car c = new Car(); Car.Exploed clientDelegate = new Car.Exploed(Program.OnDeadMethod); Car.Exploed clientDelegate2 = new Car.Exploed(Program.OnDeadMethod2); c.SetDelegateExplodedList(clientDelegate); c.SetDelegateExplodedListRemove(clientDelegate); c.SetDelegateExplodedList(clientDelegate2); //c.DelegateExplodedList = clientDelegate; //c.DelegateExplodedList = clientDelegate2; for (int i = 0; i < 10; i++) { c.Accelerate(100); } } //客户端在这里做自定义的处理 public static void OnDeadMethod(string msg) { Console.WriteLine("Client doing------>"+msg); } public static void OnDeadMethod2(string msg) { Console.WriteLine("Client Doing Type 2--------->" + msg); } }}
- callback(2)--使用代理
- callback(3)--使用方法覆盖
- callback(1)---callback interface--回传接口
- UVM的callback的使用(八)
- jQuery学习笔记(二)- 效果和Callback函数
- 使用Callback封装
- callBack的使用。。
- 使用SurfaceView.Callback
- callback函数使用
- Callback机制_实现(2)
- java备忘录(2)—回调callback
- Spring 对 hibernate 的集成(使用回调callback)
- 在VS2005中使用回调(Script Callback)
- 浅谈Android中Callback(回调)的使用
- 浅谈Android中Callback(回调)的使用
- 浅谈Android中Callback(回调)的使用
- 项目实现过程中遇到的问题记录(二)------------在Thread中使用CallBack并没有调用CallBack的返回
- jQuery plugin Validation Callback使用
- 什么是企业实现知识管理难点?如何解决?
- 收藏《CnForums和已有系统的整合方案》《通过IViewObject接口,取浏览器的图象,实现SNAP 》《SNAP的另类实现,采用iFRAME,内嵌框架的形式.》等
- 求购ERP的信息永远少一条
- 利用递归实现不定重数多重循环
- 学习mvc,hibernate,struts,spring的好书
- callback(2)--使用代理
- 建立良好的SOA体系结构对于企业至关重要
- 汹涌澎湃个人Flash网站
- Flex in Action 3-1开发MXML应用的要素和必要理论基础
- 网址引用
- 微软:将以超过1亿美元收购语义搜索引擎Powerset
- 八部众---走出软件作坊:三五个人十来条枪 如何成为开发正规军(二十三)
- 昨日之日不可留,明日之日未可知,今日之日胜千金。
- 使用try和catch