java回调机制原理与解读及举例

今天看代码时,发现自己对回调理解不清楚,特研究一下细节,看看应用场景

1.回调 最佳场景   应用于异步通讯的调用,也就是调用某一个函数,不知道什么时候有结果,但是,调用者又要知道结果  

主人请人去干活   干活的人 要回答干活的情况 这就是异步。  

java技术实现两种,   主人请人去干活-----主人亲自等待  干活人的情况   ----- 这个主实例,我这样称呼,一直要用线程去监听情况,也就是主人隔一会儿要问   这个项目进度怎么样了,

cpu的优化怎么样了?  

主人请人去干活-----派人等待  干活人的情况       ----- 专人专用  --- 这里回调就是专人专用。取名为 回调 ---实际上是项目经理  

---  项目经理要问,这个功能实现没有? 要给主人老板,这个功能差不多了,完成进度 80% 编写回调接口

 

          

package com.example.myapplication;

public interface IGetMessageCallBack {
    public void setMessage(String message);
}
package com.example.myapplication;

// 这里模拟网络传递下来的信息。。

public class updateMessage {
    private IGetMessageCallBack IGetMessageCallBack;

    public void setIGetMessageCallBack(IGetMessageCallBack IGetMessageCallBack){
        this.IGetMessageCallBack = IGetMessageCallBack;
    }

    public void updateMessage(String message){
        if (IGetMessageCallBack != null){
            IGetMessageCallBack.setMessage(message);
        }
    }
}
package com.example.myapplication;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements IGetMessageCallBack {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//      现实中,老师与学生的 问问题  与回答问题  的场景 ,进行解析,使用 回调的方法,实现,解释 回调
//      的异步通讯,同步消息到其它的场景
//      老师问 学生  今天作业做了多久?  askQuestion
//      学生徐东答 老师  今天用时 3000秒
//      学生徐茨答 老师  今天用时 5000秒
//      学生徐蓉答 老师  今天用时 8000秒
//      回答 有先有后,有快有慢,如何实现。。。

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        updateMessage mupdateMessage = new updateMessage();
        mupdateMessage.setIGetMessageCallBack(MainActivity.this);
        for (int i = 0; i < 10 ;i++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mupdateMessage.updateMessage("模拟发送消息。。。。");
        }


//        Student student_xuxiaodong = new Student_xuxiaodong();
//        Teacher teacherA = new Teacher(student_xuxiaodong);
//        Log.i("CallBack"," start AA  ....  " );
//        teacherA.askQuestion();
//
//        Student mStudent_XuChi = new Student_XuChi();
//        Teacher teacherB = new Teacher(mStudent_XuChi);
//        Log.i("CallBack"," start  BB  ....  " );
//        teacherB.askQuestion();
//
//        Student mStudent_XuRong = new Student_XuRong();
//        Teacher teacherC = new Teacher(mStudent_XuRong);
//        Log.i("CallBack"," start CC ....  " );
//        teacherC.askQuestion();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setMessage(String message) {
        Log.i("CallBack","显示 消息,看看情况 " + message );
    }
}

其实,从这个例子看 ,回调就是一个c的全局变量 一个工程中的全局变量,不知有大神能搞清楚不?学习之、分析之、使用之

猜你喜欢

转载自blog.csdn.net/u010689853/article/details/113401959
今日推荐