Java interface callback and callback Functions

Source: Internet
Author: User

Java interface callback and callback Functions

I have been developing Android for two years. I am ashamed to say that although callback can be seen everywhere, and there are a lot of online content about callback functions and interface callbacks, I have never figured it out. The reasons I cannot understand are as follows:

1. java Interface Definition and upward transformation are the basis for understanding callback;

The core reason for using interfaces: to be able to transform upwards into multiple base types. That is, multiple implementations of interfaces can be transformed to multiple interface base types.

2. Anonymous class.

Code can be seen everywhere in the new SthInterface () registration interface callback.

 

I would like to thank csdn for its two articles which allow me to thoroughly understand the callback function:

 

 

In fact, I would like to understand the online callback function example about the call request help.

Here I also wrote an example like this:

1. First define an interface (that is, the callback Interface) (the help interface can help Michael Zhang or James Li. What help do you need and bind the interface to implement it later .)

 

public interface HelperInterface {void execute();}

2. We can ask Michael to help us solve the problem (for example, Li Si or Wang Wu)

 

 

public class HelperZhangsan implements HelperInterface{@Overridepublic void execute() {System.out.println(This is zhangsan_helper.You can also ask lisi_helper!!);}}

3. For the help class, he must hold the help callback interface. Because Zhang San cannot be found, he can find Li Si. As long as the callback interface remains unchanged, he can always find the help class.

 

 

Public class Ask {private HelperInterface helperInterface; public void setHelperInterface (HelperInterface helperInterface) {// register this. helperInterface = helperInterface;} public void resultForAsk(){helperInterface.exe cute ();}}

4. Test code

 

 

public class Test {public static void main(String[] args) {Ask ask = new Ask();ask.setHelperInterface(new HelperZhangsan());ask.resultForAsk();}}

In fact, many times we prefer the following method, that is, to find an anonymous interface implementation class to help us ():

 

 

 

public class Test {public static void main(String[] args) {Ask ask = new Ask();ask.setHelperInterface(new HelperInterface() {@Overridepublic void execute() {System.out.println(hell dsc);}});ask.resultForAsk();}}

The above is the complete callback function working mechanism.

 

 

 

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.