委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法。定义一个委托的语法是类似于定义方法,只不过在声明一个委托的时候,要使用delegate关键字,如下:
public
delegate
string
MyDelegate(
string
args)
委托的名字只是一个符合,重要的是它的参数和返回值。在生成一个委托实例的时候,必须用一个方法来实例化,而这个方法的“签名”(参数和返回值)必须于委托的一致。
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class 委托 : System.Web.UI.Page
13 {
14 public class Book
15 {
16 string title;
17 int num;
18 double price;
19
20 public delegate void titlechange(); //声明titlechange这个委托
21 public event titlechange abc; //声明自定义事件abc委托给titlechange
22
23 public Book()
24 {
25 }
26 public Book(string nname, int nnum, double nprice)
27 {
28 title = nname;
29 num = nnum;
30 price = nprice;
31 }
32 public string Title
33 {
34 get
35 {
36 return title;
37 }
38 set
39 {
40 title = value;
41 abc();
42 }
43 }
44 public int Num
45 {
46 get
47 {
48 return num;
49 }
50 set
51 {
52 num = value;
53 }
54 }
55 public double Price
56 {
57 get
58 {
59 return price;
60
61 }
62 set
63 {
64 price = value;
65 }
66 }
67 public string bookinfo()
68 {
69 string html = "书名是:" + title + "<br>库存:" + num + "本" + "<br>价格为:" + price + "元";
70 return html;
71 }
72
73 }
74
75
76
77
78 protected void Page_Load(object sender, EventArgs e)
79 {
80 Book bok = new Book();
81 bok.abc+=new Book.titlechange(bok_abc);
82 bok.Title = "钢铁是怎样练成的";
83 bok.Num = 5000;
84 bok.Price = 58.55;
85 Response.Write(bok.bookinfo());
86 }
87 void bok_abc()
88 {
89 Response.Write("事件执行了<br>");
90 }
91}
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class 委托 : System.Web.UI.Page
13 {
14 public class Book
15 {
16 string title;
17 int num;
18 double price;
19
20 public delegate void titlechange(); //声明titlechange这个委托
21 public event titlechange abc; //声明自定义事件abc委托给titlechange
22
23 public Book()
24 {
25 }
26 public Book(string nname, int nnum, double nprice)
27 {
28 title = nname;
29 num = nnum;
30 price = nprice;
31 }
32 public string Title
33 {
34 get
35 {
36 return title;
37 }
38 set
39 {
40 title = value;
41 abc();
42 }
43 }
44 public int Num
45 {
46 get
47 {
48 return num;
49 }
50 set
51 {
52 num = value;
53 }
54 }
55 public double Price
56 {
57 get
58 {
59 return price;
60
61 }
62 set
63 {
64 price = value;
65 }
66 }
67 public string bookinfo()
68 {
69 string html = "书名是:" + title + "<br>库存:" + num + "本" + "<br>价格为:" + price + "元";
70 return html;
71 }
72
73 }
74
75
76
77
78 protected void Page_Load(object sender, EventArgs e)
79 {
80 Book bok = new Book();
81 bok.abc+=new Book.titlechange(bok_abc);
82 bok.Title = "钢铁是怎样练成的";
83 bok.Num = 5000;
84 bok.Price = 58.55;
85 Response.Write(bok.bookinfo());
86 }
87 void bok_abc()
88 {
89 Response.Write("事件执行了<br>");
90 }
91}