一个经典例子让你彻彻底底理解java回调机制

前端之家收集整理的这篇文章主要介绍了一个经典例子让你彻彻底底理解java回调机制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:

<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px">
以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法,这样子说你是不是有点晕晕的,其实我刚开始也是这样不理解,看了人家说比较经典的回调方式:


<ul style="font-family:Arial; font-size:14px">

  • Class A实现接口CallBack callback——背景1
  • class A中包含一个class B的引用b ——背景2
  • class B有一个参数为callback的方法f(CallBack callback) ——背景3
  • A的对象a调用B的方法 f(CallBack callback) ——A类调用B类的某个方法 C
  • 然后b就可以在f(CallBack callback)方法中调用A的方法 ——B类调用A类的某个方法D
    1.  
    2.  
    3.  
    4.  
    5. 调用函数告诉小王,也就是回调函数 
    6.  

    Highlighter bg_java" style="font-family:Consolas,160); text-decoration:none; background-color:inherit; border:none; padding:1px; margin:0px 10px 0px 0px; display:inline-block; width:16px; height:16px; text-indent:-2000px">copy
      
  • 背景一 
  •  
  • 背景二 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • "
  •  


    <div class="dp-highlighter bg_java" style="font-family:Consolas,160); text-decoration:none; background-color:inherit; border:none; padding:1px; margin:0px 10px 0px 0px; display:inline-block; width:16px; height:16px; text-indent:-2000px">copy
    <div style="position:absolute; left:469px; top:2326px; width:18px; height:18px; z-index:99">

      
  • 背景三 
  •  
  •  
  • "
  •  
  •  
  •  
  •  


    <div class="dp-highlighter bg_java" style="font-family:Consolas,160); text-decoration:none; background-color:inherit; border:none; padding:1px; margin:0px 10px 0px 0px; display:inline-block; width:16px; height:16px; text-indent:-2000px">copy
    <div style="position:absolute; left:469px; top:3080px; width:18px; height:18px; z-index:99">

      
  •  
  •  
  •  
  • 通过上面的那个例子你是不是差不多明白了回调机制呢,上面是一个异步回调,我们看看同步回调吧,onClick()方法


    <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px">
    现在来分析分析下<a target="_blank" href="http://lib.csdn.net/base/android" rel="nofollow" class="replace_word" title="Android知识库" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold">Android View的点击方法onclick();我们知道onclick()是一个回调方法,当用户点击View就执行这个方法,我们用Button来举例好了


    <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px">

    Highlighter bg_java" style="font-family:Consolas,160); text-decoration:none; background-color:inherit; border:none; padding:1px; margin:0px 10px 0px 0px; display:inline-block; width:16px; height:16px; text-indent:-2000px">copy
     
  •  
  •  
  •  
  •  

  •  
  •  
  •  
  • 背景一 
  • 背景二 
  • A类调用B类的某个方法 C 
  •  
  •  

  •  nofollow" title="在CODE上查看代码片" style="color:rgb(160,0); background-color:inherit"> * 这个View就相当于B类 
  •  
  •  
  •  
  • 背景三 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  • 调用A类的某个方法D,这个D就是所谓的回调方法

  • 方法,当执行Thread的start()方法就会回调这个run()方法,还有处理消息都比较经典等等

    错误希望指出!多谢!

    猜你在找的Java相关文章