static void Main(string[] args) { //1、初始化鱼竿 var fishingRod = new FishingRod(); //2、声明垂钓者 var jeff = new FishingMan("Ryan"); //3.分配鱼竿 jeff.FishingRod = fishingRod; ////4、注册观察者 //fishingRod.FishingEvent += new FishingEventHandler().HandleEvent; //5、循环钓鱼 while (jeff.FishCount < 5) { jeff.Fishing(); Console.WriteLine("-------------------"); //睡眠5s Thread.Sleep(5000); } Console.Read(); }
public event FishingHandler FishingEvent; //声明事件 public void ThrowHook(FishingMan man) { Console.WriteLine("开始下钩!"); //用随机数模拟鱼咬钩,若随机数为偶数,则为鱼咬钩 if (new Random().Next() % 2 == 0) { var type = (FishType) new Random().Next(0, 5); Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了"); FishingEvent?.Invoke(new FishingEventData { FishType = type, FishingMan = man }); } }