加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 06:38:04 所属栏目:Java 来源:网络整理
导读:转自: p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px" 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了

转自:

<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
  • [java]??
    ?
      ?
    1. ?
    2. ?
    3. ?
    4. ?
    5. ?

    ?
  • 背景一?
  • ?
  • 背景二?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • "
  • ?


    <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">

    ?
  • ?
  • ?
  • ?
  • ?

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

  • ?
  • ?
  • ?
  • ?
  • 背景三?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读