zoukankan      html  css  js  c++  java
  • c#.NET的事件与委托例子

    原文发布时间为:2008-07-25 —— 来源于本人的百度文章 [由搬家工具导入]

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Book bk = new Book();
            bk.nameChg+= new Book.myEventHandler(bk_nameChg);
            bk.priceChg+=new Book.myEventHandler(bk_priceChg);
            bk.Name = "c";
            bk.Price = 123;
            Response.Write(bk.bookInfo());

        }
        void bk_nameChg()
        {
            Response.Write("title change"+"<br>");
        }
        void bk_priceChg()
        {
            Response.Write("price change" + "<br>");
        }
        public class Book
        {
            private string name;
            private double price;
            public delegate void myEventHandler();
            public event myEventHandler nameChg;
            public event myEventHandler priceChg;
            public Book()
            {
            }
            public Book(string nname, double nprice)
            {
                name = nname;
                price = nprice;
            }
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                    nameChg();//这个事件触发

                }
            }

            public double Price
            {
                get
                {
                    return price;
                }
                set
                {
                    price = value;
                    priceChg();
                }
            }
            public string bookInfo()
            {
                return "title:" + name + " price:" + price;
            }

        }
    }

  • 相关阅读:
    数据结构(一)之HelloWord
    关于玩QQ消息导入导出功能的感想!
    关于java.lang.IllegalStateException
    Error querying database. Cause: java.sql.SQLException: ORA-01745: 无效的主机/绑定变量名
    关于SVN更新时文件加锁的小结
    项目中和时间相关的要注意的地方
    项目开发中遇到的小问题及小规范
    世界各国的谷歌网址
    非常不错的IT进阶站点
    CSS 垂直居中
  • 原文地址:https://www.cnblogs.com/handboy/p/7141577.html
Copyright © 2011-2022 走看看