| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 493 人关注过本帖
标题:昨天看了委托与事件,今天做了个小例子来和大家分享,希望对大家有帮助
只看楼主 加入收藏
jacky_2010
Rank: 2
等 级:论坛游民
帖 子:19
专家分:36
注 册:2010-10-13
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
昨天看了委托与事件,今天做了个小例子来和大家分享,希望对大家有帮助
程序代码:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestLibrary
{
    public class Car
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                if (value == null)
                {
                    name = "桑塔纳";
                }
                else
                {
                    name = value;
                }
            }
        }

        public void MoveCar(object sender , DriverEventArgs e)
        {
            Driver d = (Driver)sender;
            if (e.IsMove)
            {
                Console.WriteLine("{0}开动了{1}!",d.Name,name);
            }
            else
            {
                Console.WriteLine("{0}关掉了{1}引擎!", d.Name, name);
            }
        }

        public Car()
        {
            this.Name = null;
        }

        public Car(string name)
        {
            this.Name = name;
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace TestLibrary
{
    public delegate void DriverCarHandler(object sender,DriverEventArgs e);

    public class DriverEventArgs : EventArgs
    {
        private bool isMove;//是否开动汽车

        public bool IsMove
        {
            get { return isMove; }
            set { isMove = value; }
        }

        public DriverEventArgs(bool isMove)
        {
            this.IsMove = isMove;
        }
    }

    public class Driver
    {
        public event DriverCarHandler DriverCarEventHandler;

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public void DriverCar(Object sender , LightEventArgs e)
        {
            Light l = (Light)sender;
            Console.WriteLine("{0}看到交通灯信息:{1}。", name, e.Message);
            if (l.IsLight)
            {               
                Console.WriteLine("{0}兴奋的大喊道:哈,是绿灯,太走运了!", name);
            }
            else
            {
                Console.WriteLine("{0}沮丧的叹气:哎,又吃红灯了,今天真倒霉!", name);
            }
            if (DriverCarEventHandler != null)
            {
                DriverCarEventHandler(this, new DriverEventArgs(l.IsLight));
            }
        }

        private Driver() { }

        public Driver(string name)
        {
            this.Name = name;
        }
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace TestLibrary
{
    public delegate void ChangeIsLightHandler(object sender, LightEventArgs e);

    public class LightEventArgs : EventArgs
    {
        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public LightEventArgs(string message)
        {
            this.Message = message;
        }
    }
    public class Light
    {
        public event ChangeIsLightHandler ChangeIsLightEventHandler;
        private bool isLight;//True = 绿,False = 红

        public bool IsLight
        {
            get { return isLight; }
            set { isLight = value; }
        }

        public void ChangeIsLight(object sender, EventArgs e)//灯状态切换的方法
        {
            string message;
            if (isLight)
            {
                IsLight = false;
                message = "现在是红灯,请停车!";
            }
            else
            {
                IsLight = true;
                message = "现在是绿灯,请有序通过!";
            }
            if (ChangeIsLightEventHandler != null)
            {
                ChangeIsLightEventHandler(this, new LightEventArgs(message));
            }
            Console.WriteLine();
        }

        public Light() { }

        public Light(bool isLight)
        {
            this.IsLight = isLight;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using TestLibrary;
using System.Timers;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer timer = new Timer(5000);
            Light l1 = new Light();
            Light l2 = new Light(true);
            Driver d1 = new Driver("林志玲");
            Car c1 = new Car("法拉利");
            Driver d2 = new Driver("程序员");
            Car c2 = new Car();
            l1.ChangeIsLightEventHandler += d1.DriverCar;
            l2.ChangeIsLightEventHandler += d2.DriverCar;
            d1.DriverCarEventHandler += c1.MoveCar;
            d2.DriverCarEventHandler += c2.MoveCar;
            timer.Elapsed += new ElapsedEventHandler(l1.ChangeIsLight);
            timer.Elapsed += new ElapsedEventHandler(l2.ChangeIsLight);
            timer.Start();
            Console.ReadKey();
        }
    }
}
收到的鲜花
  • wangnannan2011-01-17 08:50 送鲜花  49朵   附言:很好
搜索更多相关主题的帖子: return 
2011-01-14 14:21
wy2305789
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-1-16
收藏
得分:0 
学习中。。。。。。。
2011-01-16 20:13
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
给LZ加分 继续努力啊

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2011-01-17 08:50
快速回复:昨天看了委托与事件,今天做了个小例子来和大家分享,希望对大家有帮助 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037324 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved