callback(2)--使用代理

本文介绍了一个使用C#中的代理机制的具体示例。通过创建一个Car类,并定义一个Exploed代理类型,演示了如何利用代理进行事件处理。在加速超过限制速度时触发事件,多个客户端方法可以通过简单的+=和-=操作符被添加或移除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

针对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);

      }

 

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值