callback function Learning in Java

Source: Internet
Author: User

callback function Learning in Java Blog Category:
    • J2se
Javaj# is generally divided into the following steps:
    1. The unified interface for declaring a callback function is interface A, which contains the method callback ();
    2. The interface is set to private members within the calling class caller.
    3. The public method for implementing the A interface is provided within the caller (the implementation class of the external interface is passed through the formal parameter to the caller XXX);
    4. The Xxx.callback () method is used in a caller method Dosth ();
    5. In the example of caller, a interface is implemented first, then the Dosth () method is called.

Examples of popular online:
Java code
  1. callback function interface and method
  2. Public interface Icallback {
  3. public void func ();
  4. }
  5. callback function interface Implementation class
  6. Public class Classwithcallbackfunction implements icallback{
  7. Public classwithcallbackfunction () {
  8. }
  9. public void func () {
  10. System.out.println ("CCCCCCCCCCCCCCCCCC");
  11. }
  12. }
  13. Public class Caller {
  14. private Icallback callback; //Private interface member
  15. public void Setcallback (Icallback callback) {
  16. This.callback = callback; //interface member implementation: Incoming from the outside
  17. }
  18. public void DoCallback () { //Callback interface member method
  19. Callback.func ();
  20. }
  21. }
  22. }
  23. Public class MainClass {
  24. Public MainClass () {
  25. }
  26. public static void Main (string[] args) {
  27. Caller Caller = new Caller ();
  28. Caller.setcallback (new Classwithcallbackfunction () {
  29. public void func () {
  30. System.out.println ("aaaaaaaaaa");
  31. }
  32. });
  33. Caller.docallback (); //Implementation callback
  34. }
  35. }
  36. In reality, the DoCallback () method is placed in the Setcallback call, the above is to illustrate the callback principle
  37. Public class Caller {
  38. Icallback callback;
  39. public void DoCallback () {
  40. Callback.func ();
  41. }
  42. public void Setcallback (Icallback callback) {
  43. This.callback = callback;
  44. DoCallback ();
  45. }
  46. }


Another example is the Java sort compare interface

Java code
  1. Interface Compare {
  2.    Boolean LessThan (Object LHS, object RHS);
  3.    Boolean Lessthanorequal (Object LHS, object RHS);
  4. }
  5. Import java.util.*;
  6.   Public class Sortvector extends Vector {
  7.    Private Compare Compare; //Private interface member
  8.    Public Sortvector (Compare comp) {
  9. compare = comp;
  10. }
  11.    Public void Sort () {
  12. QuickSort (0, size ()- 1);
  13. }
  14.    Private void QuickSort (int left, int. right) {
  15. if (right > left) {
  16. Object O1 = ElementAt (right);
  17. int i = left- 1;
  18. int j = right;
  19. While (true) {
  20. While (Compare.lessthan (
  21. ElementAt (++i), O1))
  22. ;
  23. While (J > 0)
  24. if (compare.lessthanorequal (
  25. ElementAt (--j), O1))
  26. Break ;
  27. if (i >= j) break ;
  28. Swap (I, j);
  29. }
  30. Swap (I, right);
  31. QuickSort (left, I-1);
  32. QuickSort (i+1, right);
  33. }
  34. }
  35. private void swap (int loc1, int loc2) {
  36. Object tmp = ElementAt (LOC1);
  37. Setelementat (ElementAt (LOC2), Loc1);
  38. Setelementat (TMP, LOC2);
  39. }
  40. }

callback function Learning in 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.