The simplest and clearest understanding of the Java callback mechanism

Source: Internet
Author: User
Tags class manager

In an example, it is easy to clearly understand the meaning and Mechanism of callback. It is important to pay attention to what the so-called "back" means.

 

The callback is based on an interface or abstract class. First, an interface for callback is shown as follows:

Package gss. callback; </p> <p>/** <br/> * interface for saving the callback method <br/> * @ author gss <br/> */< br/> public interface Icallback {<br/> public int doCallbackMethod (int, int B); <br/>}< br/>

 

 

Then there are classes that support callback:

Package gss. callback; </p> <p>/** <br/> * callback class <br/> * @ author gss <br/> */<br/> public class BackCaller {<br/> public Icallback call; <br/> private int p1; <br/> private int p2 = 10; </p> <p> public int getP1 () {<br/> return p1; <br/>}</p> <p> public void setP1 (int p1) {<br/> this. p1 = p1; <br/>}</p> <p> public int getP2 () {<br/> return p2; <br/>}</p> <p> public void setP2 (int p2) {<br/> this. p2 = p2; <br />}</P> <p> public void setCall (Icallback call) {<br/> this. call = call; <br/>}</p> <p> public void execute () {<br/> System. out. println ("Callback class execution! "); <Br/> // execute the callback method in the callback class. What is executed in this method, the scheduling process determines <br/> // the scheduling process determines what the caller is doing. This is the so-called true meaning of callback <br/> if (call! = Null) {<br/> System. out. println ("Callback method return:" + call. doCallbackMethod (p1, p2); <br/>}< br/>

 

Finally, the scheduling process is as follows:

Package gss. callback; </p> <p>/** <br/> * this class is called by the caller. For convenience, implement <br/> * @ author gss <br/> */<br/> public class Manager {</p> <p> public static void main (String args []) {<br/> int param = 100; </p> <p> BackCaller c = new BackCaller (); <br/> c. setP1 (param); </p> <p> // This is the so-called upper layer for the lower layer. <Br/> // generally, resources are transferred from the caller to the called user, and the methods are called by the caller, the called user uses the resources transferred by the caller according to their own method <br/> // callback is just the opposite. The caller determines the implementation of the method and "what is done is also provided by the caller ", the called party "returns" the logic specified by the caller <br/> // the most convenient method is to use an anonymous internal class in the call Process <br/> System. out. println ("scheduling process execution! "); <Br/> c. setCall (new Icallback () {</p> <p> public int doCallbackMethod (int a, int B) {<br/> return a * B; <br/>}</p> <p> }); <br/> // The caller can "return" here to call the logic specified by the caller <br/> c.exe cute (); </p> <p> c. setCall (new Icallback () {</p> <p> public int doCallbackMethod (int a, int B) {<br/> return a/B; <br/>}</p> <p >}); <br/> c.exe cute (); <br/>}< br/>

 

Finally, the execution result is as follows: the execution of multiplication or division is determined by the scheduling process.

Scheduling process execution! <Br/> callback class execution! <Br/> callback method return: 1000 <br/> callback class execution! <Br/> return value of the callback method: 10

 

 

Summary:

A typical example of callback is to define the interface method registered by the caller in the call process. The called user can be regarded as "back" to call the caller.

Related Article

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.