首页 > 代码库 > 代码片_记录

代码片_记录

  • 委托/事件_开水报警
技术分享
static void Main()        {            var listener = new Listener();            var boilingEvent = new TempterLisenting();            listener.Register(boilingEvent);            for (int i = 93; i < 101; i++)            {                boilingEvent.WaterTempterAdd(i);            }        }//水温事件    public class WaterTempterEventAgrs    {        private int waterTempter;        public int WaterTempter        {            get { return waterTempter; }            set { waterTempter = value; }        }        public WaterTempterEventAgrs(int waterTempter)        {            this.WaterTempter = waterTempter;        }    }    //事件类,定义事件订阅消息及触发事件    public class TempterLisenting    {        //public event EventHandler<WaterTempterEventAgrs> WaterEvent;        public delegate void WaterEventHandler(object sender, WaterTempterEventAgrs e);        public event WaterEventHandler WaterEvent;        public void WaterTempterAdd(int waterTempter)        {            if (WaterEvent != null)            {                WaterEvent(this, new WaterTempterEventAgrs(waterTempter));            }        }    }    //客户类,事件订阅者    public class Listener    {        public void IsWaterBoiling(object sender, WaterTempterEventAgrs e)        {            if (e.WaterTempter >= 90 && e.WaterTempter <= 99)            {                Console.WriteLine("the water is will boiling:{0}", e.WaterTempter);            }            else if (e.WaterTempter == 100)            {                Console.WriteLine("the water is boiling:{0}", e.WaterTempter);            }        }        public void Register(TempterLisenting listener)        {            listener.WaterEvent += IsWaterBoiling;        }    }
View Code
  •  事件与委托_C#语言程序设计基础
技术分享
namespace CodeSample{    public delegate void BookDelegate(string bookName, string bookType);    public class Custormers    {        private string custormerName;        private string custormerBooktype;        public string CustormerName        {            get { return custormerName; }            set { custormerName = value; }        }        public string CustormerBooktype        {            get { return custormerBooktype; }            set { custormerBooktype = value; }        }        public Custormers(string m_custormerName, string m_custormerBooktype)        {            custormerBooktype = m_custormerBooktype;            custormerName = m_custormerName;        }        public void Registre(BookStore bookStore)        {            bookStore.BookDelegate += BookStore_BookDelegate;        }        void BookStore_BookDelegate(string bookName, string bookType)        {            if (bookType == CustormerBooktype)            {                Console.WriteLine("{0},您好。您在本店预约的书类为\"{1}\"的书籍<<{2}>>到货了。", custormerName, bookType, bookName);            }        }    }    public class BookStore    {        public event BookDelegate BookDelegate;        public void NewBook(string bookName, string bookType)        {            BookDelegate(bookName, bookType);        }    }    public class CodeSampleMain    {        public static void Main()        {            BookStore bookStore = new BookStore();            Custormers[] custormer = new Custormers[3];            custormer[0] = new Custormers("黄**", "计算机");            custormer[1] = new Custormers("二少", "英语");            custormer[2] = new Custormers("**杰", "玄幻小说");            for (int i = 0; i < custormer.Length; i++)            {                custormer[i].Registre(bookStore);            }            bookStore.NewBook("计算机组成原理", "计算机");            bookStore.NewBook("走遍美国", "英语");            bookStore.NewBook("紫川", "玄幻小说");            Console.ReadKey();        }    }}
View Code

 

代码片_记录