<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    神奇好望角 The Magical Cape of Good Hope

    庸人不必自擾,智者何需千慮?
    posts - 26, comments - 50, trackbacks - 0, articles - 11
      BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

    用接口實現回調

    Posted on 2008-03-10 21:47 蜀山兆孨龘 閱讀(383) 評論(0)  編輯  收藏
    用接口實現回調 Implementing Callback with Interface
      C 語言里的函數指針,JavaScript 里的函數參數可以實現回調,從而完成很多動態功能。請看下面的 JavaScript 代碼: C's function pointer and JavaScript's function parameter can implement callback, accomplishing lots of dynamic functionalities. Please look at the following JavaScript code:
    1. function add(a, b) {
    2.     return a + b;
    3. }
    4. function sub(a, b) {
    5.     return a - b;
    6. }
    7. function cal(a, b, callback) {
    8.     alert(callback(a, b));
    9. }
    10. cal(2, 1, add);
    11. cal(2, 1, sub);
    12. cal(2, 1, function (a, b) {
    13.     return a * b;
    14. });
      在對 cal 函數的三次調用中,變量 callback 分別指向三個函數(包括一個匿名函數),從而在運行時產生不同的邏輯。如果仔細研究網上各種開源的 JS 庫,會發現大量此類回調。 In the three invokings to function cal, variable callback points to three different functions (including one anonymous function), which generates different logics at runtime. If you study various open source JS libraries on the Internet in depth, you will find many callbacks of this kind.
      Java 語言本身不支持指針,所以無法像 JavaScript 那樣將方法名直接作為參數傳遞。但是利用接口,完全可以達到相同效果: Java language itself doesn't support pointer, so the method name can't be directly passed as a parameter like JavaScript. But with interface, the completely same effect can be achieved:
    1. public interface Cal {
    2.     public int cal(int a, int b);
    3. }
    4. public class Add implements Cal {
    5.     public int cal(int a, int b) {
    6.         return a + b;
    7.     }
    8. }
    9. public class Sub implements Cal {
    10.     public int cal(int a, int b) {
    11.         return a - b;
    12.     }
    13. }
    14. public class Test {
    15.     public static void main(String[] args) {
    16.         test(2, 1, new Add());
    17.         test(2, 1, new Sub());
    18.         test(2, 1, new Cal() {
    19.             public int cal(int a, int b) {
    20.                 return a * b;
    21.             }
    22.         });
    23.     }
    24.     private static void test(a, b, Cal c) {
    25.         System.out.println(c.cal(a, b));
    26.     }
    27. }

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 无码AV片在线观看免费| 美女被免费网站91色| 日韩亚洲人成在线综合日本 | 国产AV无码专区亚洲AV手机麻豆| 亚洲久热无码av中文字幕| 国产美女在线精品免费观看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲Av无码国产情品久久| 美国毛片亚洲社区在线观看| 日本人护士免费xxxx视频| 亚洲AV成人无码网天堂| 一本久久综合亚洲鲁鲁五月天 | 成人免费一区二区三区| 国产av无码专区亚洲av桃花庵| 免费无码黄网站在线看| 色噜噜综合亚洲av中文无码| 成人免费的性色视频| 亚洲高清一区二区三区电影| 免费jjzz在在线播放国产| 久久性生大片免费观看性| 亚洲欧洲日产国码久在线观看| 在线看免费观看AV深夜影院 | 在线播放免费人成视频网站| 亚洲午夜久久久久久久久久 | 午夜老司机免费视频| 日韩免费在线中文字幕| 亚洲av无码不卡| AV片在线观看免费| 无遮挡免费一区二区三区| 亚洲色图在线播放| 成人免费午夜视频| xxxxxx日本处大片免费看| 亚洲精品视频免费看| 免费高清资源黄网站在线观看| eeuss免费天堂影院| 亚洲图片校园春色| 亚洲人成人无码网www国产| 91人成网站色www免费下载| 国产精品亚洲一区二区无码| 久久精品国产亚洲香蕉| 在线观看免费精品国产|