C#学习[1]--C#的事件和委托的实现和思考

      假如类A要和类B保持事件关联,如果A发生了事情,类B的函数就要进行响应。那么就可以采用委托的方式,实现这个功能。

      在A类中创建一个委托,这个委托和B中的响应函数的外表一模一样(类型和传的参数),然后在类B中将委托实例化,并被委托的方法进行关联(实例化和关联通常是一起进行的)。关联之后这种A发生事件,B类的函数可以响应了。

      具体的例子,是A是一个钟表类,可以获得当前的时间,B是一个显示时间的类,这个时候A的时间每次发生变化,B作为显示方法,就要进行刷新显示。 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace CsharpApplication3
{
    public class TimeInfoEventArgs : EventArgs
    {
        public int hour;
        public int minute;
        public int second;
        public TimeInfoEventArgs(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
        
    }

    //发布类
    public class Clock
    {
        private int hour;
        private int minute;
        private int second;

        public delegate void SecondChangeHandler(object clock, TimeInfoEventArgs timeInformation);//声明一个委托
        public SecondChangeHandler SecondChanged;     //对委托进行实例化,实例化对象为secondhandler

        public void Run()
        {
            for (; ; )
            {
                // 避免刷新次数过多,定为每100毫秒进行一次检查和刷新判断
                Thread.Sleep(100);
                System.DateTime dt = System.DateTime.Now;//获取当前时间
                if (dt.Second != second)//如果当前时间和自己所存时间信息不同,那么就获得最新的时间信息 
                {
                    TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second);
                    if (SecondChanged != null)//如果委托实例不为空,那么就调用委托方法,将最新信息传参到Display函数中。
                    {
                        SecondChanged(this, timeInformation);//this是发布源,timeinfo是要传递的时间信息等参数。委托实例化见DisplayClock函数
                    }
                }
                this.second = dt.Second;
                this.minute = dt.Minute;
                this.hour = dt.Hour;//将当前类中的私有变量进行更新。

            }
        }
    }
    public class DisplayClock
    {
        public void Subscribe(Clock theClock)
        {
            theClock.SecondChanged += new Clock.SecondChangeHandler(TimeHasChanged);
           //通过+=操作符创建委托实例,并且获得TimehasChanged的委托和管理权限,方便之后Clock类传参进来。
        }

         public void TimeHasChanged(object theClock, TimeInfoEventArgs ti)
        {
            Console.WriteLine("Current Time {0}:{1}:{2}", ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString());
        }

    }

    public class Tester
    {
        public void Run()
        {
            Clock theClock = new Clock();

            DisplayClock dc = new DisplayClock();
            dc.Subscribe(theClock);

 
            theClock.Run();
        }
    }

    public class Program
    {
        public static void Main()
        {
            Tester t = new Tester();
            t.Run();
        }
    }
 

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值