The design of interface callback is very important in practical application , Its concept is difficult to understand from the perspective of words

Then the following is a simple practical application case

proposal :

Follow up and knock , Draw call graph , Then look at the notes and understand them over and over again

Case requirements :

Boss Yue ordered the driver to repair the car ,

During the period, boss Yue was busy with his own business ,

The driver reports to boss Yue after repairing the car ;

code implementation :

Callback interface
/** * Callback function interface */ public interface CallBack { /** * Reporting methods , * This is the callback method *
When the driver finishes repairing the car, he will call this method on his own initiative , Report to the boss * Why callback ? * Boss succession CallBack Interface * this report() The method belongs to the boss *
The boss ordered the driver to do something , Call the driver's repair method , The driver will call the boss's method to report when he is done , The way the boss is called in turn is “ Callback method ” * This is the callback *
You text people , Others receive text messages and reply to you through your mobile phone number , Your mobile phone number is the callback method , Send and receive through mobile phone number * @param str */ public void
report(String str); }
Boss
/** * Boss * The boss only inherited the callback interface , You can use the callback method */ public class Boss implements CallBack {
/** * private Driver driver; This is understood as : * You need to call the driver's repair method , You have to have a driver , Otherwise, it cannot be called */ private
Driver driver; public Boss(Driver driver){ this.driver=driver; } /** * Repair method *
The boss ordered the driver to repair the car in this way */ public void orderFix(){ System.out.println(" Boss Yue told the driver to repair the car ");
// Create a new thread , Call driver's repair method , // The realistic understanding is : Does not interfere with the boss's thread , The boss should continue to do his own business new Thread(()->driver.FixCar(
this)).start(); } /** * This is the callback method , It belongs to the boss * The driver will call this method after repairing the car * @param str */ @Override
public void report(String str) { System.out.println(str); } public void doOther(
){ System.out.println(" Boss Yue went to do other things of his own ..........."); } }
Drivers
/** * Drivers */ public class Driver { /** * Driver's repair method * Callback interface as parameter * The purpose is to call the callback method , Report to the boss
* @param callBack */ public void FixCar(CallBack callBack){ try { // Thread sleep , Simulate the repair process
Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }
// After the repair , Call callback method , Inform the boss that the car has been repaired callBack.report(" Driver Report : boss , The car has been fixed for you , I slipped first "); } }
Test class
/** * Test class */ public class TestDemo { public static void main(String[] args) {
Driver driver=new Driver(); Boss yue=new Boss(driver); // Order the driver to repair the car , Report it to the boss when it's fixed yue.
orderFix(); // The boss is busy with his own business yue.doOther(); } }
Running results :

Boss Yue told the driver to repair the car
Boss Yue went to do other things of his own ...........

( The process of the driver repairing the car .....)

Driver Report : boss , The car has been fixed for you , I slipped first

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element