.net框架笔记(六)之-------三

本文通过模拟电话响铃这一事件,详细介绍了C#中事件(Event)的定义与使用方法。包括事件的触发、监听及响应过程,并通过电话、主人和老鼠三个类的关系展示了事件的实际应用场景。

接上面的:

我在一次面试中:遇到过这样的一道题目:

猫叫了一声,主人被惊醒了,所有的老鼠都跑了.

现在用事件来写下,肯定有不妥之处,希望能够指教:

一:

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1
 2    //这把电话比做猫了,不好意思
 3   public class Phone
 4ExpandedBlockStart.gifContractedBlock.gif    {
 5        private int _height;
 6        private int _number;
 7
 8        public int Number
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        {
10ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _number; }
11ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _number = value; }
12        }

13
14        public int Height
15ExpandedSubBlockStart.gifContractedSubBlock.gif        {
16ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _height; }
17ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _height = value; }
18        }

19
20       public event EventHandler Call;
21
22       protected virtual void OnCall(EventArgs e)
23ExpandedSubBlockStart.gifContractedSubBlock.gif       {
24           if (Call != null)
25ExpandedSubBlockStart.gifContractedSubBlock.gif           {
26               Call(this,e);
27           }

28       }

29       public void PhoneCall()
30ExpandedSubBlockStart.gifContractedSubBlock.gif       {
31           Console.WriteLine("电话响了,主人听到了");
32           OnCall(new EventArgs());
33       }

34
35       
36    }

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1//主人类
 2   public class Person
 3ExpandedBlockStart.gifContractedBlock.gif    {
 4        private string _name;
 5        private int _number;
 6
 7        public string Name
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _name; }
10ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _name = value; }
11        }

12        
13
14        public int Number
15ExpandedSubBlockStart.gifContractedSubBlock.gif        {
16ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _number; }
17ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _number = value; }
18        }

19
20       //要登记事件了,要不没人通知电话来了啊
21       public Person(Phone phone)
22ExpandedSubBlockStart.gifContractedSubBlock.gif       {
23           phone.Call += new EventHandler(phone_Call);
24       }

25
26       public void phone_Call(object sender, EventArgs e)
27ExpandedSubBlockStart.gifContractedSubBlock.gif       {
28           Console.WriteLine("主人知道了,马上过去");
29       }

30
31
32    }

 

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1 //老鼠类
 2   public class Mouse
 3ExpandedBlockStart.gifContractedBlock.gif    {
 4        private int _count;
 5
 6        public int Count
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _count; }
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _count = value; }
10        }

11
12       public Mouse(Phone phone)
13ExpandedSubBlockStart.gifContractedSubBlock.gif       {
14           phone.Call += new EventHandler(phone_Call);
15       }

16
17       public void phone_Call(object sender, EventArgs e)
18ExpandedSubBlockStart.gifContractedSubBlock.gif       {
19           Console.WriteLine("电话响了,主人醒来了,老鼠逃跑了");
20       }

21    }

 

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1   class Program
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3        static void Main(string[] args)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 5            Phone phone = new Phone();
 6            Person person = new Person(phone);
 7            Mouse mouse = new Mouse(phone);
 8
 9            phone.PhoneCall();
10        }

11    }

二:这个是自身触发

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1 public class Phone
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3        private int _height;
 4        private int _number;
 5
 6        public int Number
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 8            get
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            {
10                return _number; 
11            }

12            set
13ExpandedSubBlockStart.gifContractedSubBlock.gif            {
14                if (_number !=value)
15ExpandedSubBlockStart.gifContractedSubBlock.gif                {
16                    _number = value;
17                    OnCall(new EventArgs());
18                }

19                
20            }

21        }

22
23        public int Height
24ExpandedSubBlockStart.gifContractedSubBlock.gif        {
25ExpandedSubBlockStart.gifContractedSubBlock.gif            get return _height; }
26ExpandedSubBlockStart.gifContractedSubBlock.gif            set { _height = value; }
27        }

28       public Phone()
29ExpandedSubBlockStart.gifContractedSubBlock.gif       
30           this.Call += new EventHandler(Phone_Call);//先登记,后触发
31          
32           
33       }

34
35       public Phone(int number)
36ExpandedSubBlockStart.gifContractedSubBlock.gif       {
37           this.Call+=new EventHandler(Phone_Call);
38           this.Number = number;
39           
40 
41       }

42
43      public void Phone_Call(object sender, EventArgs e)
44ExpandedSubBlockStart.gifContractedSubBlock.gif       {
45           Console.WriteLine("电话响了,号码为{0}",this.Number);
46       }

47
48       public event EventHandler Call;
49
50       protected virtual void OnCall(EventArgs e)
51ExpandedSubBlockStart.gifContractedSubBlock.gif       {
52           if (Call != null)
53ExpandedSubBlockStart.gifContractedSubBlock.gif           {
54               Call(this, e);
55           }

56       }

57       public void PhoneCall()
58ExpandedSubBlockStart.gifContractedSubBlock.gif       {
59           OnCall(new EventArgs());
60       }

61     
62       
63    }

64class Person
65ExpandedBlockStart.gifContractedBlock.gif    {
66        public Person(Phone phone)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        {
68            phone.Call += new EventHandler(phone_Call);
69        }

70
71       public void phone_Call(object sender, EventArgs e)
72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
73            Console.WriteLine("主人也登记了事件");
74        }

75    }

76 class Program
77ExpandedBlockStart.gifContractedBlock.gif    {
78        static void Main(string[] args)
79ExpandedSubBlockStart.gifContractedSubBlock.gif        {
80            Phone phone = new Phone();
81            Phone phone1 = new Phone(112222);
82            //Person person = new Person(phone);
83            Person person = new Person(phone);
84            phone.PhoneCall();
85        }

86    }

 

希望能够指教.谢谢~

转载于:https://www.cnblogs.com/dreamersjun/archive/2008/09/29/1301881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值