//注意 以下代码均是讲解的最终版本,详细讲解内容,请关注直播
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(); //我们利用委托和事件 让我们解耦更彻底,让我们的代码更简洁 //休息十分钟,稍后回来 // } } }