标签:object module com art add wal 代码 back nbsp
参考文章:http://blog.csdn.net/wufeng4552/article/details/6261779
代码实现以下功能:主人回来了,猫叫了一声,老鼠被吓跑了同事吵醒了小孩。
以下是类图:
以下是实现代码:
‘‘‘ <summary> ‘‘‘ 主人 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Class Master ‘‘‘ <summary> ‘‘‘ 主人到家事件 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Event ReachHomeEvent As ReachHomeEventHandler ‘‘‘ <summary> ‘‘‘ 主人回家过程 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Sub BackHome() Walk() RaiseEvent ReachHomeEvent(Me) End Sub ‘‘‘ <summary> ‘‘‘ 步行 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Sub Walk() Console.WriteLine("步行回家") Threading.Thread.Sleep(800) End Sub End Class Public Delegate Sub ReachHomeEventHandler(ByVal sender As Object)
‘‘‘ <summary> ‘‘‘ 猫 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Class Cat ‘‘‘ <summary> ‘‘‘ 猫叫 ‘‘‘ Master.ReachHomeEvent事件的事件处理程序 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Sub Mao(ByVal sender As Object) Console.WriteLine("Miao") RaiseEvent MaoEvent(Me, New EventArgs()) End Sub ‘‘‘ <summary> ‘‘‘ 猫叫事件 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Event MaoEvent As EventHandler End Class
Public Class Mouse Public Sub Escape(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("猫来了,赶紧跑!") End Sub End Class
Public Class Children ‘‘‘ <summary> ‘‘‘ 小孩哭 ‘‘‘ Master.ReachHomeEvent的事件处理程序 ‘‘‘ </summary> ‘‘‘ <remarks></remarks> Public Sub Cry() Console.WriteLine("呜呜呜") End Sub ‘‘‘ <summary> ‘‘‘ 小孩醒来 ‘‘‘ </summary> ‘‘‘ <param name="sender"></param> ‘‘‘ <param name="e"></param> ‘‘‘ <remarks></remarks> Public Sub WakeUp(ByVal sender As Object, ByVal e As EventArgs) Cry() End Sub End Class
Module Module1 Private WithEvents _master As Master Private WithEvents _cat As Cat Private WithEvents _child As Children Sub Main() Dim _mouse As Mouse _master = New Master() _cat = New Cat() _child = New Children() _mouse = New Mouse() ‘_cat监视_master AddHandler _master.ReachHomeEvent, AddressOf _cat.Mao ‘_mouse监视_cat AddHandler _cat.MaoEvent, AddressOf _mouse.Escape ‘_child监视_cat AddHandler _cat.MaoEvent, AddressOf _child.WakeUp _master.BackHome() Console.ReadKey() End Sub End Module
标签:object module com art add wal 代码 back nbsp
原文地址:http://www.cnblogs.com/bob-bao/p/6382185.html