using System;
using System.Collections.Generic;
using System.Text;namespace ss
{
    public enum comparsion
    {
        theFirstComesFirst=1,theSecondComesFirst=2
    }
    public class Delegate
    {
        //存放两个对象的私有数组
        private object[] thePair = new object[2];
        //声明代理
        public delegate comparsion WhichIsFirst(object obj1, object obj2);
        //构造方法,参数为两个对象
        //按接收顺序添加
        public Delegate(object firstObiect, object secondObject)
        {
            thePair[0] = firstObiect;
            thePair[1] = secondObject;
        }
        //公有方法,按对象具体范畴进行排序
        public void Sort(WhichIsFirst theDelegateFunc)
        {
            if (theDelegateFunc(thePair[0], thePair[1]) == comparsion.theSecondComesFirst)
            {
                object temp = thePair[0];
                thePair[1] = thePair[0];
                thePair[0] = temp;
            }
        }
        //公共方法,按对象具体范畴进行逆序排序
        public void ReverseSort(WhichIsFirst theDelagateFunc)
        {
            if (theDelagateFunc(thePair[0], thePair[1]) == comparsion.theFirstComesFirst)
            {
                object temp = thePair[1];
                thePair[1] = thePair[0];
                thePair[0] = temp;
            }
        }
        //转换成字符串值
        public override string ToString()
        {
            return thePair[0].ToString() + ',' + thePair[1].ToString();
        }        public class Dog
        {
            private int weight;
            public Dog(int weight)
            {
                this.weight = weight;
            }
            //Dog按体重排序
            public static comparsion WhichDogComesFirst(object o1, object o2)
            {
                Dog d1 = (Dog)o1;
                Dog d2 = (Dog)o2;
                return d1.weight > d2.weight ? comparsion.theSecondComesFirst : comparsion.theFirstComesFirst;
            }
            public override string ToString()
            {
                return weight.ToString();
            }
        }        public class Student
        {
            private string name;
            Student(string name)
            {
                this.name = name;
            }
            public static comparsion WhichStudentComesFirst(object o1, object o2)
            {
                Student s1 = (Student)o1;
                Student s2 = (Student)o2;
                return (string.Compare(s1.name, s2.name) < 0) ? comparsion.theFirstComesFirst : comparsion.theSecondComesFirst;
            }
            public override string ToString()
            {
                return name;
            }
        }       public class Test
        {            static void Main(string[] args)
            {
                //创建俩个Student对象和Dog
                //加入delegate对象
                Student BechHam = new Student("BeckHam");
                Student Ronaldo = new Student("Ronaldo");
                Dog YellowDog = new Dog(56);
                Dog BlackDog = new Dog(12);
                Delegate stuPair = new Delegate(BechHam, Ronaldo);
                Delegate dogPair = new Delegate(YellowDog, BlackDog);
                Console.WriteLine("stuPair\t\t\t:{0}", stuPair.ToString());
                Console.WriteLine("dogPair\t\t\t:{0}", dogPair.ToString());
                //实例化代理
                Delegate.WhichIsFirst theStudentDelegate = new Delegate.WhichIsFirst(Student.WhichStudentComesFirst);
                Delegate.WhichIsFirst theDogDelegate = new WhichIsFirst(Dog.WhichDogComesFirst);
                //用代理排序
                stuPair.Sort(theStudentDelegate);
                Console.WriteLine("After Sort studentPair\t\t{0}", stuPair.ToString());
                stuPair.ReverseSort(theStudentDelegate);
                Console.WriteLine("After Reversesort studentPair\t\t{0}", stuPair.ToString());
                dogPair.Sort(theDogDelegate);
                Console.WriteLine("After Sort dogPair\t\t{0}", dogPair.ToString());
                dogPair.ReverseSort(theDogDelegate);
                Console.WriteLine("After RecerseSort dogPair\t\t{0}", dogPair.ToString());
            }        }    }
}提示错误为:错误 1 “ss.Delegate”没有合适的静态 Main 方法
请求高手帮帮忙了。

解决方案 »

  1.   

    楼主的代码在vs2008中编译显示 "ss.ss.Delegate.Student”不包含采用“1”参数的构造函数。
    是因为Delegate.Student类的构造函数没有加public,因而默认为internal,所以不能在Test.Main中访问。
      

  2.   

    刚闲着无事, 看了一下, 共5个错误:1. 把最后行的: } 去掉.
    2. public class Dog 行前少了一个:  }
    3. public class Student 类中的第三行:  Student(string name)  改为: public  Student(string name) 
    4. 代码部分倒数第10行
                   Delegate.WhichIsFirst theDogDelegate = new WhichIsFirst(Dog.WhichDogComesFirst);
       替换成:
       Delegate.WhichIsFirst theDogDelegate = new Delegate.WhichIsFirst(Dog.WhichDogComesFirst);
    5. 最后添加一行代码便于你查看运行结果: Console.ReadKey();
      

  3.   


    楼上的改变楼主的意图了。第1、2条的}是对的,这使得Student和Dog类都是Delegate类的嵌套类。
    第三条才是问题的关键,就是public构造函数的问题。第四条没有必要,因为WhichIsFirst是public的。至于第五条,说不定楼主用的IDE能自动等待用户输入呢
      

  4.   

    多谢各位了,代码有错误,就各位说的那样,还有就是自己的错误了,吧启动项选择错了,我选成ss.Delegate了。呵呵
      

  5.   

     把Student和Dog类这两个凤牛马不相及的类都作为Delegate嵌套类?当然这也可以,但个人感觉不太妥当,而且这样做也把程序入口点: static void Main(string[] args) 也包含过去了.
    这样似乎不太像 '面向对象' 的程序,不知你是否也这样认为? 个人观点, 请大家指教.