标签:style class blog code java http
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 委托练习 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 string s = Console.ReadLine(); 13 if (s == "1") 14 { 15 SomeDelegate f = Hello; 16 } 17 else 18 { 19 SomeDelegate f = Wow; 20 } 21 //两处错误:1、没有调用方法。2、变量f的作用域 22 Console.Read(); 23 } 24 25 static void Hello(string name) 26 { 27 Console.WriteLine("Hello:{0}", name); 28 } 29 30 static void Wow(string name) 31 { 32 Console.WriteLine("Wow:{0}", name); 33 } 34 } 35 36 delegate void SomeDelegate(string name); 37 }
正解::
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 委托练习 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 string s = Console.ReadLine(); 13 14 SomeDelegate f;//解决问题2,变量作用域 15 if (s == "1") 16 { 17 f = Hello;//只是指向,没有执行 18 } 19 else 20 { 21 f = Wow; 22 } 23 f("aaa");//调用f指向的函数//解决问题一,方法调用 24 Console.Read(); 25 } 26 27 static void Hello(string name) 28 { 29 Console.WriteLine("Hello:{0}", name); 30 } 31 32 static void Wow(string name) 33 { 34 Console.WriteLine("Wow:{0}", name); 35 } 36 } 37 38 delegate void SomeDelegate(string name); 39 }
标签:style class blog code java http
原文地址:http://www.cnblogs.com/skyl/p/3785141.html