callback function for a thorough understanding of Java

Source: Internet
Author: User

The callback function, first seen in the Java programming idea, and later the Observer pattern also applied the idea of the callback function. But has been no attention, and finally in an interview at the time of the loss, more and more understand why many people say Java programming ideas this book to look over and over, thoroughly understand!

First search on the network a lot of articles about what is a callback function, see Foggy. Later, the step by step decomposition finally understood, recorded as follows.

Extract the definition from Wikipedia: (read this article to understand the definition)

in the Computer Programming The callback function , or callback, refers to the function Parameters a piece of code passed to another Executable Code of the References . This design allows the underlying code to invoke subroutines defined at the top level .

In Java, we use interfaces to implement callbacks.

The explanations in this article are very well defined: http://blog.csdn.net/sweetwxh/article/details/2067139

the so-called callback is that programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. 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.

[Java]View Plaincopy
  1. Public class Caller {
  2. private Mycallinterface Callinterface;
  3. Public Caller () {
  4. }
  5. public void Setcallfunc (Mycallinterface callinterface) {
  6. this.callinterface = callinterface;
  7. }
  8. public Void Call () {
  9. Callinterface.printname ();
  10. }
  11. }


2. Of course, the definition of the interface is required, in order to facilitate programmer B to write a program based on my definition interface.

[Java]View Plaincopy
    1. Public interface Mycallinterface {
    2. public void Printname ();
    3. }


3. The third is the definition of programmer b written by program B

[Java]View Plaincopy
    1. Public class Client implements Mycallinterface {
    2. @Override
    3. public void Printname () {
    4. System.out.println ("This is theclient Printname method");
    5. }
    6. }


4. Test the following

[Java]View Plaincopy
    1. Public class Test {
    2. public static void Main (string[] args) {
    3. Caller Caller = new Caller ();
    4. Caller.setcallfunc (new Client ());
    5. Caller.call ();
    6. }
    7. }


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.

[Java]View Plaincopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. Caller Caller = new Caller ();
  4. Caller.setcallfunc (New Client ());
  5. Caller.setcallfunc (new Mycallinterface () {
  6. public void Printname () {
  7. System.out.println ("This is theclient Printname method");
  8. }
  9. });
  10. Caller.call ();
  11. }
  12. }


Read the above article, directly read the following article: http://kidult.iteye.com/blog/148982

The above is the full understanding of the callback function.

callback function for a thorough understanding of Java

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.