首页 新闻 会员 周边

net EventArgs 的疑惑 突然发现微软搞了这么多什么 都是xxxx

0
悬赏园豆:5 [已解决问题] 解决于 2013-01-25 13:34

EventArgs 的解释:这里借鉴网上的。

说的通俗一些,就是: 
有一个叫做EventHandler 的家伙,他会告诉你(主程序),有一些事情发生了:这个事情是谁导致的呢?是某个object类型对象导致的,它用Source或Sender来表示。这个事情是什么事呢?e的内容就是事情的内容了。

总结一句 就是对象的信息。

接下来看代码 你会发现前面的都是多余的 (代码也是在网上学习 突然有感而发)。

 1 namespace tspevent
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Heater heater = new Heater();
 8             Alarm alarm = new Alarm();
 9 
10             heater.Boiled += alarm.MakeAlert;
11             heater.Boiled += Display.ShowMsg;
12             heater.BoilWater();    
13         }
14     }
15 
16     public class Heater
17     {
18         private int temp;
19         public string type = "tsp", area = "china";
20         public delegate void tspdEventHandler(Object sender, tspEventArgs e);
21         public event tspdEventHandler Boiled;
22 
23         public class tspEventArgs : EventArgs
24         {
25             public readonly int temp;
26             public tspEventArgs(int temp)
27             {
28                 this.temp = temp;
29             }
30         }
31 
32         protected void OnBolied(tspEventArgs e)
33         {
34             if (Boiled != null)
35             {
36                 Console.WriteLine("---"+this);
37                 Boiled(this, e);    
38             }
39         }
40 
41         public void BoilWater()
42         {
43             for (int i = 0; i <= 100; i++)
44             {
45                 temp = i;
46                 if (temp > 95)
47                 {
48                     tspEventArgs e = new tspEventArgs(temp);
49                     OnBolied(e);
50                 }
51             }
52         }
53     }
54 
55     public class Alarm
56     {
57         public void MakeAlert(Object sender, Heater.tspEventArgs e)
58         {
59             Heater heater = (Heater)sender;    
60             Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type);
61             Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temp);
62             Console.WriteLine();
63         }
64 
65     }
66 
67     public class Display
68     {
69         public static void ShowMsg(Object sender, Heater.tspEventArgs e)
70         {    
71             Heater heater = (Heater)sender;
72             Console.WriteLine("Display:{0} - {1}: ", heater.area, heater.type);
73             Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", e.temp);
74             Console.WriteLine();
75         }
76     }
77 }

结果:

 

接下来看简单的改下 你会发现net里面有个不怎么常用的this(如果用js比较多朋友会对this非常的熟悉 )

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Heater heater = new Heater();
            Alarm alarm = new Alarm();
            heater.Boiled += alarm.MakeAlert;
            heater.Boiled += Display.ShowMsg;
            heater.BoilWater();    
        }
    }

    public class Heater
    {
        public int temp;
        public string type = "tsp", area = "china";
        public delegate void tspdEventHandler(object e);
        public event tspdEventHandler Boiled;
        public void BoilWater()
        {
            for (int i = 0; i <= 100; i++)
            {
                temp = i;
                if (temp > 95)
                {
                    if (Boiled != null)
                    {
                        Boiled(this);
                    }
                }
            }

        }

    }

    public class Alarm
    {
        public void MakeAlert(Object obj)
        {
            Heater heater = (Heater)obj;    
            Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type);
        }

    }

    public class Display
    {
        public static void ShowMsg(Object obj)
        {
            Heater heater = (Heater)obj;
            Console.WriteLine("Display:{0} - {1}-{2}: ", heater.area, heater.type, heater.temp);
        }
    }
}

 

结果 

发现微软绕了这么大的弯子 就一个this就解决了,也许还有人说xxx其他的 我在来个net3.5的扩展方法的代码 按照前面的意思 你怎么不搞EventArgs 呢? 

namespace ExtensMethods
{
    sealed class tsp
    {
        private double d1, d2, d3;

        public tsp(double d1, double d2, double d3)
        {
            this.d1 = d1;
            this.d2 = d2;
            this.d3 = d3;
        }

        public double sum()
        {
            return d1 + d2 + d3;
        }
    }

    static class extens
    {
        public static double avg(this tsp sp)//注意这个 this
        {
            return sp.sum() / 3;
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            tsp sp = new tsp(1, 2, 3);
            Console.WriteLine("sun={0}", sp.sum());
            Console.WriteLine("avg={0}", sp.avg());
        }
    }
}

核心就是一个this 。

问题 就是this可以实现EventArgs 一样的功能 甚至比它还强大 我就不明白还出来一个EventArgs干嘛? 搞的那么多人不理解

s_p的主页 s_p | 初学一级 | 园豆:138
提问于:2013-01-23 18:23
< >
分享
最佳答案
0

private void Form1_Load(object sender, EventArgs e)

看到第一个参数 sender 了吗?这不就是你的“this”?

请仔细阅读下观察着模式,观察者模式中有提到,如果观察者只关心目标对象的某个方面(aspects),可以将观察者对象注册为目标对象的某特定事件感兴趣:

void Subject::Attatch(Observer*,Aspect& interest)

那么目标对象将这方面的改变作为通知中的一个参数提供给它的观察者:

void Observer::Update(Subject*,Aspect& interest);

这是一种扩展实现,基本实现就是不提供对目标的引用,而是只提供事件参数:

void Observer::Update(Aspect& interset);

此种实现表明,interest 是同目标对象分离的,也就是说在观察者处理此事件的时候,不依赖于目标对象当前的状态。

收获园豆:5
Launcher | 高人七级 |园豆:45045 | 2013-01-24 09:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册