设计模式连载:第四篇还有七分钟,远古巨龙即将复活,请勇士们做好准备
来源:互联网 发布:wind数据试用账号 编辑:程序博客网 时间:2024/05/01 04:23
//注意 以下代码均是讲解的最终版本,详细讲解内容,请关注直播
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace Design_pro_four{ //第四篇:远古巨龙还有七分钟刷新,请各位勇士做好准备(开播时间:16年6月19日晚上七点半) public abstract class hero { protected string name; protected iMonster im; public hero(string name, iMonster im) { this.name = name; this.im = im; } abstract public void receive_mes(); } public class warrior : hero { public warrior(string name,iMonster im) : base(name,im) { } public override void receive_mes() { Console.WriteLine("战士{0}得到了消息{1}{2}", name,im.Name,im.State); } public void warrior_recive_mes() { Console.WriteLine("战士独特{0}得到了消息{1}{2}", name, im.Name, im.State); } } public class yellow_archr : hero { public yellow_archr(string name, iMonster im) : base(name, im) { } public override void receive_mes() { Console.WriteLine("弓箭手{0}得到了消息{1}{2}", name, im.Name, im.State); } public void archr_recive_mes() { Console.WriteLine("弓箭手独特{0}得到了消息{1}{2}", name, im.Name, im.State); } } public class magic : hero { public magic(string name, iMonster im) : base(name, im) { } public override void receive_mes() { Console.WriteLine("魔法师{0}得到了消息{1}{2}", name, im.Name, im.State); } } public class paladin : hero { public paladin(string name, iMonster im) : base(name, im) { } public override void receive_mes() { Console.WriteLine("圣骑士{0}得到了消息{1}{2}", name, im.Name, im.State); } } public class summoner: hero { public summoner(string name, iMonster im) : base(name, im) { } public override void receive_mes() { Console.WriteLine("召唤师{0}得到了消息{1}{2}", name, im.Name, im.State); } public void sum_recive_mes() { Console.WriteLine("召唤师独特{0}得到了消息{1}{2}", name, im.Name, im.State); } } public interface iMonster{ string Name { set; get; } string State { set; get; } void notify(); } //定义了一个委托变量 public delegate void receive_delegate(); //声明事件 public class dragon : iMonster { //用委托和事件写的 public event receive_delegate receive_event; private string _name; private string _state; public dragon(string _name, string _state) { this._name = _name; this._state = _state; } public string Name { get { return _name; } set { _name = value; } } public string State { get { return _state; } set { _state = value; } } public void notify() { receive_event(); } } public class shark_boss : iMonster { //英雄列表 public List<hero> hero_list = new List<hero>(); private string _name; private string _state; public shark_boss(string _name,string _state) { this._name = _name; this._state = _state; } public string Name { get { return _name; } set { _name = value; } } public string State { get { return _state; } set { _state = value; } } public void Add(hero _hero) { hero_list.Add(_hero); } public void del(hero _hero) { hero_list.Remove(_hero); } public void notify() { foreach (var a in hero_list) { a.receive_mes(); } } } public class cyclops: iMonster { //英雄列表 public List<hero> hero_list = new List<hero>(); private string _name; private string _state; public cyclops(string _name, string _state) { this._name = _name; this._state = _state; } public string Name { get { return _name; } set { _name = value; } } public string State { get { return _state; } set { _state = value; } } public void Add(hero _hero) { hero_list.Add(_hero); } public void del(hero _hero) { hero_list.Remove(_hero); } public void notify() { foreach (var a in hero_list) { a.receive_mes(); } } } class City { static void Main(string[] args) { dragon fire_dragon = new dragon("火龙", "我又复活了!!!"); shark_boss shark1 = new shark_boss("鲨鱼辣椒", "我要统治你们"); cyclops cy1 = new cyclops("独眼巨人", "啦啦啦啦德玛西亚"); warrior Jackw = new warrior("杰克",fire_dragon); yellow_archr luobinghan = new yellow_archr("罗兵汉",shark1); // yellow_archr aixi = new yellow_archr("艾希"); magic m1 = new magic("蓝袍甘道夫",shark1); // magic m2 = new magic("金袍甘道夫"); // magic m3 = new magic("绿袍甘道夫"); // paladin pa1 = new paladin("黄金圣骑士"); summoner sum1 = new summoner("黑龙召唤师",fire_dragon); fire_dragon.receive_event += new receive_delegate(Jackw.warrior_recive_mes); fire_dragon.receive_event += new receive_delegate(sum1.sum_recive_mes); fire_dragon.receive_event -= new receive_delegate(Jackw.warrior_recive_mes); fire_dragon.notify(); //我们利用委托和事件 让我们解耦更彻底,让我们的代码更简洁 //休息十分钟,稍后回来 // } }}
0 0
- 设计模式连载:第四篇还有七分钟,远古巨龙即将复活,请勇士们做好准备
- JavaScript入门篇--请做好准备
- java请做好准备
- java23种设计模式七(连载)
- 设计模式笔记之6原则--为“变化”做好准备
- 设计模式准备篇
- 【JavaScript笔记一】请做好准备
- DevEdge 即将复活!
- 大数据时代,请做好持久战的准备!
- JavaScript入门-请做好准备_1.1热身
- JavaScript入门-第1章 请做好准备
- 设计模式还有行为模式。。。
- 私有云架构连载 – 第四部分(终篇):设计模式
- 做好准备
- 做好准备
- 做好准备
- 做好准备
- 设计模式学习笔记-准备篇
- 十九. 增加一个项目协作留言板功能(五)----- 增加添加附件功能
- 获取Object的所有keys or values
- 设计模式连载:第三篇我要变得更强
- 安装hadoop-hama
- Surprise Language Round #8 题解(待补)
- 设计模式连载:第四篇还有七分钟,远古巨龙即将复活,请勇士们做好准备
- 由递推关系到通项公式
- leetcode_c++:Contains Duplicate II(219)
- 第十三周项目3
- python 列表推导式----轻量级循环
- UVa 10905
- 图像检索
- 分布式事务
- 二十. 增加一个项目协作留言板功能(六)----- 增加一个任务结束按钮