溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

委托二--------使用委托實現(xiàn)冒泡排序

發(fā)布時間:2020-06-22 05:51:29 來源:網(wǎng)絡(luò) 閱讀:670 作者:1473348968 欄目:編程語言

-------------------------------------------------------------Proson.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    public class Proson
    {
        public string Name { get; private set; }
        public decimal Money { get; private set; }
        public Proson(string name, decimal money)
        {
            this.Name = name;
            this.Money = money;
        }
        public override string ToString()
        {
            return string.Format("姓名:{0};月薪:{1}", Name, Money);
        }
        //此方法用于傳為委托
        public static bool IsMax(Proson p1, Proson p2)
        {
            return p1.Money < p2.Money;
        }
    }
}

-------------------------------------------------------------MySort.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    public class MySort
    {
        //IList<T> array  :用于接收任何數(shù)組,集合
        //Func<T, T, bool> func :委托 ,傳遞兩個參數(shù),一個bool返回值
        public static void Sort<T>(IList<T> array, Func<T, T, bool> func)
        {
            bool yesno = true;
            do {
                yesno = false;
                for (int i = 0; i < array.Count - 1; i++)
                {
                    if (func(array[i], array[i + 1]))//用到了委托【bool IsMax(Proson p1, Proson p2)】
                    {
                        T temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                        yesno = true;
                    }
                }
            } while (yesno);
        }
    }
}

-------------------------------------------------------------主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Proson[] p = { new Proson("張翼德", 100000m), 
                             new Proson("趙子龍", 50000m),
                             new Proson("關(guān)云長", 150000m), 
                             new Proson("諸葛亮", 200000m) };
            //傳遞一個數(shù)組,和一個方法
            MySort.Sort(p, Proson.IsMax);
            foreach (var item in p)
            {
                Console.WriteLine(item.ToString());
                Console.ReadLine();
            }
 
        }
    }
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI