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

©2019-2020 Toolsou All rights reserved,
The 11th Blue Bridge Cup python The real topic of the University Group National Games JavaSwing To achieve a simple Lianliankan games 【Spring Source code analysis 】42-@Conditional Detailed explanation element-ui Step on pit record 2019PHP Interview questions ( Continuously updated )PHPJava Misunderstanding —— Method overloading is a manifestation of polymorphism ? First issue 500 100 million , Set up a new Department , What is Tencent going to do ? Google chrome The browser can't open the web page , But what if other browsers can open it ? Regression of dependent variable order categories (R language )【Golang Basic series 10 】Go language On conditional sentences if