黑马程序员_委托、多播委托、委托与指针的区别

一、委托

1.将方法当做变量来使用的一种机制(作为回调方法、实现事件)|将函数以参数形式传到别的方法里|在一个类里执行另外一个类的私有方法。

2.委托是一种类型

3.语法:[访问修饰符]delegate 类型名 委托名(参数列表)

4.使用:准备方法->准备委托类型->定义委托变量->使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 比较算法
 7 {
 8     class Program
 9     {
10         #region 准备委托类型
11         public delegate bool DeleCompare(string str1, string str2); 
12         #endregion
13 
14         static void Main(string[] args)
15         {
16             while (true)
17             {
18                 string str1 = "20";
19                 string str2 = "100";
20                 Console.WriteLine("请问你要用什么方式进行比较:(0、数字;1、字符串)");
21                 string res = Console.ReadLine();
22                 #region 定义委托变量
23                 DeleCompare dc; 
24                 #endregion
25                 #region 定义委托变量
26                 if (res == "0")
27                 {
28                     //装配方法
29                     dc = Compare1;
30                 }
31                 else if (res == "1")
32                 {
33                     //装配方法
34                     dc = Compare2;
35                 }
36                 else
37                 {
38                     dc = null;
39                     Console.WriteLine("输入有误,请重新输入。");
40                     Console.ReadKey(true);
41                     Console.Clear();
42                     continue;
43                 } 
44                 #endregion
45                 string str = "";
46 
47                 //开始比较
48                 #region 使用
49                 str = dc(str1, str2) ? ">" : "<"; 
50                 #endregion
51                 Console.WriteLine("{0}{1}{2}", str1, str, str2);
52                 Console.ReadKey(true);
53                 Console.Clear();
54             }
55         }
56         #region 准备方法
57         //比较数字的大小
58         static bool Compare1(string str1, string str2)
59         {
60             int num1 = int.Parse(str1);
61             int num2 = int.Parse(str2);
62             return num1 > num2;
63         }
64         //比较字符串的大小
65         static bool Compare2(string str1, string str2)
66         {
67             if (string.Compare(str1, str2) > 0)
68             {
69                 return true;
70             }
71             return false;
72         }
73         #endregion
74     }
75 }
比较算法
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 委托学习
 7 {
 8     public delegate void DeleShow();
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //DeleShow ds = Show2.ShowIn;
14             //Show1.ShowOut(ds);
15             DeleShow ds;
16             Console.WriteLine("你想做什么?
1.打印字符
2.打开记事本");
17             string str=Console.ReadLine();
18             switch (str)
19             {
20                 //这里就好像是多态,但是这里是同一个方法(Show1.ShowOut())展现出不同方法体的特征,
21                 //就像是多态的方法表现形式一样。
22                 case "1": ds = Show2.ShowIn; break;
23                 case "2": ds = Open.OpenNotepad; break;
24                 default: ds = null; break;
25             }
26             Show1.ShowOut(ds);
27             Console.ReadKey(true);
28         }
29     }
30     class Show1
31     {
32         public static void ShowOut(DeleShow show)
33         {
34             //在ShowOut这个方法体里面调用在其它类下的其它方法。委托的一种实现。
35             Console.WriteLine("==================");
36 
37             //当show没有赋值的时候,或者赋值表达式不由你进行控制的时候,你是不知道show会执行什么的,那么这样就灵活多了。
38             //如果在这里直接写ShowIn方法,就写死了,灵活度就降低了。
39             show();
40             Console.WriteLine("==================");
41         }
42     }
43     class Show2
44     {
45         public static void ShowIn()
46         {
47             Console.WriteLine("欢迎来到黑马训练营");
48         }
49     }
50     class Open
51     {
52         public static void OpenNotepad()
53         {
54             System.Diagnostics.Process.Start("notepad.exe");
55         }
56     }
57 }
委托学习
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 自定义排序
 7 {
 8     public delegate bool DelegateSortStr(string str1, string str2);
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             DelegateSortStr DgS = SortStr;
14             string[] str = { "5464", "786783", "4234", "12", "7576", "564", "976", "63", "5" };
15             for (int i = 0; i < str.Length - 1; i++)
16             {
17                 for (int j = 0; j < str.Length - 1 - i; j++)
18                 {
19                     if (DgS(str[j], str[j + 1]))
20                     {
21                         string temp = str[j];
22                         str[j] = str[j + 1];
23                         str[j + 1] = temp;
24                     }
25                 }
26             }
27             foreach (string temp in str)
28             {
29                 Console.WriteLine(temp);
30             }
31             Console.ReadKey(true);
32         }
33         //static bool SortNum(string str1, string str2)
34         //{
35         //    return int.Parse(str1) > int.Parse(str2);
36         //}
37         static bool SortStr(string str1, string str2)
38         {
39             return string.Compare(str1, str2) > 0 ? true : false;
40         }
41     }
42 }
自定义排序

二、多播委托(委托链)

1.每次加上一个委托就会创建一委托对象

2.委托链其中一个出现异常,那么后面的将不再执行

3.委托有返回值时,只返回最后一个

4.对于访问委托链中的每一个的返回结果,使用GetInvocationList()得到一个Delegate数组(委托基类),使用的时候必须强转为定义的委托类型。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace 多播委托
 7 {
 8     public delegate int DelegateDemo();
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             DelegateDemo DgDemo = Func1;
14             DgDemo += Func2;
15             DgDemo += Func3;
16             DgDemo += Func4;
17             DgDemo += Func5;
18             //多播委托,依次执行所有方法
19             //多播委托只返回最后一个方法的值
20             Console.WriteLine(DgDemo());
21             //想要返回所有方法,则必须用到委托的GetInVoCationList()方法将每个结果返回            
22             Delegate[] DS = DgDemo.GetInvocationList();
23             foreach (Delegate temp in DS)
24             {
25                 //输出的时候必须强转回定义的委托类型
26                 Console.WriteLine(((DelegateDemo)temp)());
27             }
28             Console.ReadKey(true);
29         }
30         static int Func1()
31         {
32             Console.WriteLine("Func1");
33             return 1;
34         }
35         static int Func2()
36         {
37             Console.WriteLine("Func2");
38             return 2;
39         }
40         static int Func3()
41         {
42             Console.WriteLine("Func3");
43             return 3;
44         }
45         static int Func4()
46         {
47             Console.WriteLine("Func4");
48             return 4;
49         }
50         static int Func5()
51         {
52             Console.WriteLine("Func5");
53             return 5;
54         }
55         //输出结果:
56         //Func1
57         //Func2
58         //Func3
59         //Func4
60         //Func5
61         //5
62         //Func1
63         //1
64         //Func2
65         //2
66         //Func3
67         //3
68         //Func4
69         //4
70         //Func5
71         //5
72     }
73 }

三、委托与指针的区别

1.委托是面向对象的,是一个对象。委托比较安全,在定义时规定了类型,不是所有方法都可以(指向),委托是安全的类型。

2.指针是函数的首地址,是指向一个地址。通过指针可以访问函数的地址,指针有空类型,可以指向任何一段地址。

原文地址:https://www.cnblogs.com/dlwcg/p/3615035.html