委托创建小记

 

 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4
 5 namespace  MyDelegates
 6 {
 7    class Program
 8    {
 9        static void Main(string[] args)
10        {
11            Test.main();
12            Console.WriteLine();
13            Test2.main();
14            Console.Read();
15        }

16    }

17
18    public delegate int PrintMessageHandler(string message);
19
20    public class Test
21    {
22        static int Print(string msg)
23        {
24            Console.WriteLine(msg);
25            return msg.Length;
26        }

27
28        static void PrintTitle(PrintMessageHandler prn)
29        {
30            prn("=======================");
31            prn("       XYZ Bank        ");
32            prn("=======================");
33        }

34
35        public static void main()
36        {
37            PrintMessageHandler prn = new PrintMessageHandler(Print);
38            string s = "    直接使用委托实例    ";
39            int n = prn(s);
40            PrintTitle(prn);
41        }

42    }

43
44    public delegate int printmessagehandler(string message);
45
46    public class PrintProvider
47    {
48        public int Print(string msg)
49        {
50            Console.WriteLine(msg);
51            return msg.Length;
52        }

53    }

54
55    public class PrintConsumer
56    {
57        public void PrintTitle(printmessagehandler prn)
58        {
59            prn("====================");
60            prn("         XYZ Bank1     ");
61            prn("====================");
62        }

63    }

64    public class Test2
65    {
66        public static void main()
67        {
68            PrintProvider pp = new PrintProvider();
69            printmessagehandler prn = new printmessagehandler(pp.Print);
70            string s = "直接使用委托实例";
71            int n = prn(s);
72            PrintConsumer pc = new PrintConsumer();
73            pc.PrintTitle(prn);
74        }

75    }

76}

77
78

你可能感兴趣的:(创建)