问题描述
usingSystem;namespaceDelegate{///<summary>///Class1的摘要说明。///</summary>///classClass1{///<summary>///应用程序的主入口点。///</summary>[STAThread]staticvoidMain(string[]args){////TODO:在此处添加代码以启动应用程序//PeopleGreeting("好人",ChineseGreeting);Console.ReadLine();}publicdelegatevoidGreetingDelegate(stringname);publicvoidEnglishGreeting(stringname){Console.WriteLine("morning"+name);}publicvoidChineseGreeting(stringname){Console.WriteLine("早上好~"+name);}privatestaticvoidPeopleGreeting(stringname,GreetingDelegateGreetingMethod){GreetingMethod(name);}}}我用的是VS2003
解决方案
解决方案二:
[STAThread]staticvoidMain(string[]args){////TODO:在此处添加代码以启动应用程序//PeopleGreeting("好人",a);Console.ReadLine();}publicdelegatevoidGreetingDelegate(stringname);publicvoidEnglishGreeting(stringname){Console.WriteLine("morning"+name);}publicvoidChineseGreeting(stringname){Console.WriteLine("早上好~"+name);}GreetingDelegatea=newGreetingDelegate(ChineseGreeting);ChineseGreetingb=newChineseGreeting(EnglishGreeting);privatestaticvoidPeopleGreeting(stringname,GreetingDelegateGreetingMethod){GreetingMethod(name);}
解决方案三:
//输出:早上好~好人//如果你用PeopleGreeting("好人",b);//输出:morning好人
解决方案四:
GreetingDelegate和类有相似之处,在使用之前是需要实例化的:PeopleGreeting("好人",newGreetingDelegate(ChineseGreeting));PeopleGreeting方式的第二个参数是一个委托型的实例,你不能直接传递方法,而是应该先创建一个GreetingDelegate的实例,将你真正需要调用的方法ChineseGreeting传给该委托的实例,否则委托并不知道它应该调用哪个方法。回忆一下在winform中是如何使用事件的吧,比如一个按钮的点击事件:button.Click+=newEventHandler(click_process_method_name);EventHandler实际上也是一个委托,因此我们也同样需要先将它实例化
解决方案五:
以上两个方法都不成功啊~~
解决方案六:
我用这个方法作出来了classClass1{///<summary>///应用程序的主入口点。///</summary>[STAThread]staticvoidMain(string[]args){////TODO:在此处添加代码以启动应用程序//Class1cls=newClass1();GreetingDelegatea=newGreetingDelegate(cls.EnglishGreeting);GreetingDelegateb=newGreetingDelegate(cls.ChineseGreeting);PeopleGreeting("好人",b);Console.ReadLine();}publicdelegatevoidGreetingDelegate(stringname);publicvoidEnglishGreeting(stringname){Console.WriteLine("morning"+name);}publicvoidChineseGreeting(stringname){Console.WriteLine("早上好~"+name);}privatestaticvoidPeopleGreeting(stringname,GreetingDelegateGreetingMethod){GreetingMethod(name);}}类的实例中的方法进行绑定
解决方案七:
此题LZ好好翻下书这个问题就解决了你这样上来发贴求解还是不会牢固的