Use of the Java callback method

Source: Internet
Author: User

Use of callback methods
1, Introduction: Program A exposed a method a out. In the B program call the A method, B to implement the corresponding logic.

Instance:
1), define an interface
Public interface IAA {
public void message ();
}
2) Define a class that exposes the show method in the class.
public class AA {
Private IAA IAA;
public void Addlistner (IAA IAA) {
THIS.IAA=IAA;
}
public void Show () {
This.iaa.message ();
}
}
3), define BB class
3.1),
public class BB {
public static void Main (string[] args) {
AA aa=new AA ();
Anonymous inner class
Aa.addlistner (New IAA () {
B program to implement its own logic code
@Override
public void message () {
System.out.println ("Adsfasdf");
}
});
Aa.addlistner (New BB ());
Call the method exposed by a program.
Aa.show ();
}
}
3.2),
public class BB implements iaa{
@Override
public void message () {
System.out.println ("bbbbbbbb");
}
}

public static void Main (string[] args) {
AA aa=new AA ();
Aa.addlistner (New BB ());
Call the method exposed by a program.
Aa.show ();
}
Note: The events that are added to Android can be very good for the use of callback methods or for fragment and activity communication.
Ajax access to the server and so on are used to the callback function.


Use 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.