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 方法
请求高手帮帮忙了。
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 方法
请求高手帮帮忙了。
解决方案 »
- 如何给混淆后的程序集加强名称?
- 高分急求解!!我的类库里的类我现在访问不到!
- 怎么实现复制,剪切,粘贴啊?
- 急!!!!C#邮件发送的问题~~~高手们进,散分啦!!!!!!
- 请问在一个线程中如何显示窗体?
- 关于using System.Net.Sockets;的问题,请各位高手给些指点,麻烦了!!!很急。
- float a = float.Parse(TextBox1.ToString());为什么会说 输入字符串的格式不正确
- 请问高手门如何在工具栏的下拉按钮中显示mdi子窗体列表啊,我用了上下文菜单,但是不管用,高手帮忙解决一下嘛,谢谢!!
- 求助:关于DataGridView行充满控件的问题
- 如何只取日期,不要时间,谢谢
- XML 档案读写,求教大侠,读到List中老是有问题!!
- DllImport(),能不能引用相对路径的dll
是因为Delegate.Student类的构造函数没有加public,因而默认为internal,所以不能在Test.Main中访问。
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();
楼上的改变楼主的意图了。第1、2条的}是对的,这使得Student和Dog类都是Delegate类的嵌套类。
第三条才是问题的关键,就是public构造函数的问题。第四条没有必要,因为WhichIsFirst是public的。至于第五条,说不定楼主用的IDE能自动等待用户输入呢
这样似乎不太像 '面向对象' 的程序,不知你是否也这样认为? 个人观点, 请大家指教.