• 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;
            }

        }
    }

  • 相关阅读:
    496. 下一个更大元素 I
    240. 搜索二维矩阵 II
    java反射之ObjectAnalyzer
    PHP导出excel文件的多种方式
    git获取公钥和私钥以及常用的命令
    PHP PSR-2 代码风格规范
    phpStrom安装PHP_CodeSniffer检查代码规范
    常见PHP安全网站漏洞及防范措施
    Oracle中创建主键并在Spring data JPA中使用
    JPA自定义查询中报错:缺失右括号
  • 原文地址:https://www.cnblogs.com/handboy/p/7141577.html
Copyright © 2020-2023  润新知