public delegate void catMiaoHanler(Cat cat); public class Cat { public catMiaoHanler thisHanler; public string Name { get; set; } public Cat() { } public void Miao() { Console.WriteLine("miao miao miao "); thisHanler(this); } } class Mouse { public string Name { get; set; } public int Level { get; set; } public Mouse(Cat listenCat) { listenCat.thisHanler += (cat) => { if (Level > 10) Console.WriteLine(cat.Name + "is coming!!!" + this.Name + " go go go "); else Console.WriteLine(cat.Name + "is coming!!!" + this.Name + " is dead!"); }; } } class Program { static void Main(string[] args) { //Cat cat = new Cat(new List<Mouse> { new Mouse { Name = "tom", Level = 1 }, new Mouse { Name = "jack", Level = 2 } }); Cat cat = new Cat { Name = "Tim" }; Mouse m1 = new Mouse(cat) { Name = "tom", Level = 9 }; Mouse m2 = new Mouse(cat) { Name = "tom2", Level = 19 }; cat.Miao(); Console.ReadLine(); } }
miao miao miao
Timis coming!!!tom is dead!
Timis coming!!!tom2 go go go