callback function Tutorial (i): callback function definition and an instance implemented with an interface

Source: Internet
Author: User

callback function Definition:

Pass a function A to another function B, and this function B executes function A. Let's just say that function A is called a callback function. If there is no name, it is called an anonymous callback function. Or pass a method object A to another method object B, and let the latter execute a at the appropriate time, then a is called a callback function.

The callback function instance understands:

Firemen give a phone number 119 (which everyone knows, 119 is equivalent to an interface in Java), and each place has a fire detection device, if the danger to people's lives, then the public can be 119 to the firemen call, Then call a fireman to handle it. The callback means that the fireman gave us a fire call and we could use this number to call the firemen in the event of a fire, but it was not us who dealt with the problem, but the firemen. We are the equivalent of a feedback, only responsible for the firemen feedback information, and ultimately the handler is a fireman.

The code is as follows:

1. Define the callback interface and the method that will be executed in the callback, that is, the method of dialing a distress call.

Public interface Mycallback {       void call ();        
2. Define a class that can operate on the callback interface
public class Caller {       private mycallback mycallback;            public void Docall () {          mycallback.call ();     }        public void Setmycallback (Mycallback mycallback) {          this.mycallback = mycallback;      }        }  
3. Instantiate a specific callback function, implement a callback method, tell the fireman to fire the fire
public class Callbacktest {public      static void Main (string[] args) {                    Caller Caller = new Caller ();                Caller.setmycallback (New Mycallback () {              @Override public              Void Call () {                  System.out.println (' help ');              }          });                    Caller.docall ();      }        }  

callback function Tutorial (i): callback function definition and an instance implemented with an interface

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.