zoukankan      html  css  js  c++  java
  • 猫与老鼠的故事(委托)

    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

  • 相关阅读:
    用免费Scrum工具Leangoo思维导图 实现影响地图
    mysql-线程模型
    mongodb-锁
    mongodb-mmapv1存储引擎解析(转)
    netty-read
    netty-bind
    netty-eventloop
    java基础-Executor
    ehcache3-源码简析三
    ehcache3-源码简析二
  • 原文地址:https://www.cnblogs.com/luxiaobin/p/4125995.html
Copyright © 2011-2022 走看看