日期:2014-05-17  浏览次数:20474 次

委托的问题
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Employee2
{

    public delegate bool MyDelegate(object a, object b);

    class Program
    {
       
        static void Main(string[] args)
        {
            Employee[] e = new Employee[5];            
            for(int i=0;i<5;i++)
            {
                Console.WriteLine("输入姓名:");
                string n = Convert.ToString(Console.ReadLine());
                Console.WriteLine("输入薪水:");
                double s=Convert.ToDouble(Console.ReadLine());
               [color=#FF0000] //想要在这边把n,s赋值到e的Name,Salary中去,[/color]
            }
         [color=#FF0000]   //还有这边的new Employee().Compare,提示没有参数,不知道如何解决[/color]
            MyDelegate md1 = new MyDelegate( new Employee().Compare);
            BubbleSorter.Sort(e, md1);
            
        }

    }
    class Employee
    {
        private string name;
        private double salary;

        public Employee(string Name, double Salary)
        {
            Name = name;
            Salary = salary;
        }
        public bool Compare(object a, object b)
        {
            if (((Employee)a).salary > ((Employee)b).salary)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    //排序
    class BubbleSorter
    {
        public static void Sort(object[] sortArray, MyDelegate md)
        {
           
            for (int i = 0; i < sortArray.Length; i++)
                for (int j = i + 1; j < sortArray.Length; j++)
                    if (md(sortArray[i], sortArray[j]))
                    {
                        object temp = sortArray[i];
                        sortArray[i] = sortArray[j];
                        sortArray[j] = temp;
                    }
        }
    }
}


请求指点

------解决方案--------------------
e[i].name=n;
e[i].salary=s;
------解决方案--------------------
果然一样啊,那我就赚分了哈~~~