代碼很簡單,不多做解釋,如果有疑問和建議請留言,回第一時間回覆 C#代碼first class Program { static void Main(string[] args) { MyCallback mc = new MyCallback(); mc.Callback(new Action(s ...
代碼很簡單,不多做解釋,如果有疑問和建議請留言,回第一時間回覆
C#代碼first
class Program { static void Main(string[] args) { MyCallback mc = new MyCallback(); mc.Callback(new Action(success), new Action(failed)); mc.Callback(new Action(() => { Console.WriteLine("無參匿名success"); }), new Action(() => { Console.WriteLine("無參匿名failed"); })); mc.Callback(new Action<object>(success), new Action<object>(failed)); Console.ReadKey();//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請註明出處,謝謝! } static void success() { Console.WriteLine("無參success"); } static void failed() { Console.WriteLine("無參failed"); } static void success(object str) { Console.WriteLine((string)str); } static void failed(object str) { Console.WriteLine((int)str); } } class MyCallback { public void Callback(Action success, Action failed) { success(); failed();//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請註明出處,謝謝! } public void Callback(Action<object> success, Action<object> failed) { success("123"); failed(456); } }
JS代碼
<script type="text/javascript"> var mc = new MyCallback(); mc.Callback(success, failed); console.log(222); function success(str) { console.log(str); } function failed(num) { console.log(num); } mc.Callback(function (str) { console.log(str); }, function (num) { console.log(num);//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請註明出處,謝謝! }) console.log(333); function MyCallback() { this.Callback = function (success, failed) {//無參,有參 自己選擇傳不傳參 successDelegate = success;//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請註明出處,謝謝! failedDelegate = failed; setTimeout('successDelegate("111")', 1000); //非同步 setTimeout('failedDelegate("444")', 4000);//非同步 // success("123");//同步 // failed(456);//同步 } } </script>