Understanding of the Java callback method

Source: Internet
Author: User

I used to see the word "callback method (or callback function)", but I didn't know what it meant, and I didn't understand how to use it. Now from the network to collect some good information, and then tidy up, as their own notes, also as a learning process of a small footprint. (Most of this note comes from other people's summaries, questions, answers, etc, hereby stated)

Below from 1. What is a callback function? 2. What is the function of callback functions? 3. What is the specific implementation of the callback function? (through the example solution) three aspects to elaborate:

I. What is a callback function?

  1. Wikipedia definition (read this article to understand): in computer programming , the callback function (callback) refers to the function parameters passed to other code, a piece of A reference to the executable code . This design allows the underlying code to invoke subroutines defined at the top level.

  2. Other definitions (easy to understand, recommended): the so-called callback, is the process sequencer a wrote a program ( program a), which reserved a callback function interface , and encapsulated the program. Programmer B is going to have a call to a method in its own program B , so he callbacks himself through the interface in a , B. methods in the. (See the third part of the routine)

3. the core of the callback : A program is used as a member variable to use the program on a specific occasion. (This member variable is implemented through the interface in Java)

Two. What is the function of callback functions?

The callback method is a kind of loosely coupled design idea which separates function definition and function realization. (This part of the present understanding, to be supplemented)

Three. What is the specific implementation of the callback function?

  In Java, we use interfaces to implement callbacks.

 Here is an example:

1. First define a class caller, according to the above definition is programmer A written by program a, this class to save an interface reference .

 public  class   Caller {  Mycallinterface Callinterface;  public   Caller () {
}
public void Setcallfunc ( Mycallinterface callinterface) { Callinterface; public void call () {callinterface.printname (); }}

explanation of the Setcallfunc () method in the above code: the callback is to pass the code into the object, then how to pass the code into it, of course, to make a class, a method in the class is the code. So for the caller there are two points, one is how to get this class, and the other is how to know what the method is. For the 1th, you see the set method that sends the external class as a property. As for the 2nd, you need to define an interface and then call the method defined in the interface.

2. Of course, the definition of the interface is required, in order to facilitate the process Sequencer B According to my definition of programming implementation interface.

 Public Interface Mycallinterface {    publicvoid  printname ();}

  

3. The third is to define program B written by programmer B.

 Public class Implements mycallinterface {    @Override    publicvoid  printname () {        System.out.println ("This is the client Printname method");}    }

  

4. Test the following

 Public class Test {    publicstaticvoid  main (string[] args) {        new Caller ();        Caller.setcallfunc (new  Client ());        Caller.call ();    }}

See here should understand what is callback, some articles introduced very good, but at first did not understand, is because the 3rd step of the class omitted, directly written anonymous class.

5. Use anonymous classes directly in the test method, eliminating the 3rd step.

 public  class   Test { public  static  void   main (string[] args) {Caller Caller  = new   caller ();  //         Caller.setcallfunc (new Client ());  Caller.setcallfunc (new   Mycallinterface () { /span>public  void   Printname () {System.out.println () the client Printname method            ");        }        });    Caller.call (); }}

Understanding of the Java callback method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.