c#委托的用处: 我仅仅知道委托是将方法作为参数,求进一步指导
答案:2 悬赏:40
解决时间 2021-02-26 10:22
- 提问者网友:鐵馬踏冰河
- 2021-02-26 06:11
c#委托的用处: 我仅仅知道委托是将方法作为参数,求进一步指导
最佳答案
- 二级知识专家网友:千夜
- 2021-02-26 06:51
public delegate bool ComparisonHandler(int first, int second);//委托类型的声明
class BubbleSort1
{
public static bool GreaterThan(int first, int second)//升序排序
{
return first > second;
}
public static bool LessThan(int first, int second)//降序排序
{
return first < second;
}
public static void BubbleSort(int[] items, ComparisonHandler comparisonMethod)
{
int i, j, temp;
if (items == null)
{
return;
}
if (comparisonMethod == null)
{
throw new ArgumentNullException("comparisonMehtod is NULL !");
}
for (i = items.Length - 1; i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (comparisonMethod(items[j - 1], items[j]))
{
temp = items[j - 1];
items[j - 1] = items[j];
items[j] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
int intcount;
Console.WriteLine("Please input the count of an Integer to sort which the sequence number:");
intcount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("
Please input an Integer to sort sequence:");
int[] items = new int[intcount];
for (int i = 0; i < intcount; i++)
{
items[i] = Convert.ToInt32(Console.ReadLine());
}
ComparisonHandler comparisonMethod = BubbleSort1.GreaterThan;
BubbleSort1.BubbleSort(items, comparisonMethod);
Console.WriteLine("
The sorting method in the calling class BubbleSort1 sorted sequence of integers as follows:");
Console.WriteLine("
Ascending Order:");
for (int i = 0; i < intcount; i++)
{
Console.Write(items[i] + " ");
}
Console.ReadKey();
ComparisonHandler comparisonMethodLess = BubbleSort1.LessThan;
BubbleSort1.BubbleSort(items, comparisonMethodLess);
Console.WriteLine("
");
Console.WriteLine("Descending Order:");
for (int i = 0; i < intcount; i++)
{
Console.Write(items[i] + " ");
}
Console.ReadKey();
}
}
//一个委托实例;追问这么长追答这只是一个小Demo,里面没有多少内容的,就是一个数组的排序;
只说概念有点抽象,这个Demo便于理解概念。
class BubbleSort1
{
public static bool GreaterThan(int first, int second)//升序排序
{
return first > second;
}
public static bool LessThan(int first, int second)//降序排序
{
return first < second;
}
public static void BubbleSort(int[] items, ComparisonHandler comparisonMethod)
{
int i, j, temp;
if (items == null)
{
return;
}
if (comparisonMethod == null)
{
throw new ArgumentNullException("comparisonMehtod is NULL !");
}
for (i = items.Length - 1; i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (comparisonMethod(items[j - 1], items[j]))
{
temp = items[j - 1];
items[j - 1] = items[j];
items[j] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
int intcount;
Console.WriteLine("Please input the count of an Integer to sort which the sequence number:");
intcount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("
Please input an Integer to sort sequence:");
int[] items = new int[intcount];
for (int i = 0; i < intcount; i++)
{
items[i] = Convert.ToInt32(Console.ReadLine());
}
ComparisonHandler comparisonMethod = BubbleSort1.GreaterThan;
BubbleSort1.BubbleSort(items, comparisonMethod);
Console.WriteLine("
The sorting method in the calling class BubbleSort1 sorted sequence of integers as follows:");
Console.WriteLine("
Ascending Order:");
for (int i = 0; i < intcount; i++)
{
Console.Write(items[i] + " ");
}
Console.ReadKey();
ComparisonHandler comparisonMethodLess = BubbleSort1.LessThan;
BubbleSort1.BubbleSort(items, comparisonMethodLess);
Console.WriteLine("
");
Console.WriteLine("Descending Order:");
for (int i = 0; i < intcount; i++)
{
Console.Write(items[i] + " ");
}
Console.ReadKey();
}
}
//一个委托实例;追问这么长追答这只是一个小Demo,里面没有多少内容的,就是一个数组的排序;
只说概念有点抽象,这个Demo便于理解概念。
全部回答
- 1楼网友:杯酒困英雄
- 2021-02-26 08:18
动态调用方法时方便。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |