callback function Learning in Java
Blog Category:
Javaj# is generally divided into the following steps:
- The unified interface for declaring a callback function is interface A, which contains the method callback ();
- The interface is set to private members within the calling class caller.
- 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);
- The Xxx.callback () method is used in a caller method Dosth ();
- In the example of caller, a interface is implemented first, then the Dosth () method is called.
Examples of popular online:
Java code
- callback function interface and method
- Public interface Icallback {
- public void func ();
- }
- callback function interface Implementation class
- Public class Classwithcallbackfunction implements icallback{
- Public classwithcallbackfunction () {
- }
- public void func () {
- System.out.println ("CCCCCCCCCCCCCCCCCC");
- }
- }
- Public class Caller {
- private Icallback callback; //Private interface member
- public void Setcallback (Icallback callback) {
- This.callback = callback; //interface member implementation: Incoming from the outside
- }
- public void DoCallback () { //Callback interface member method
- Callback.func ();
- }
- }
- }
- Public class MainClass {
- Public MainClass () {
- }
- public static void Main (string[] args) {
- Caller Caller = new Caller ();
- Caller.setcallback (new Classwithcallbackfunction () {
- public void func () {
- System.out.println ("aaaaaaaaaa");
- }
- });
- Caller.docallback (); //Implementation callback
- }
- }
- In reality, the DoCallback () method is placed in the Setcallback call, the above is to illustrate the callback principle
- Public class Caller {
- Icallback callback;
- public void DoCallback () {
- Callback.func ();
- }
- public void Setcallback (Icallback callback) {
- This.callback = callback;
- DoCallback ();
- }
- }
Another example is the Java sort compare interface
Java code
- Interface Compare {
- Boolean LessThan (Object LHS, object RHS);
- Boolean Lessthanorequal (Object LHS, object RHS);
- }
- Import java.util.*;
- Public class Sortvector extends Vector {
- Private Compare Compare; //Private interface member
- Public Sortvector (Compare comp) {
- compare = comp;
- }
- Public void Sort () {
- QuickSort (0, size ()- 1);
- }
- Private void QuickSort (int left, int. right) {
- if (right > left) {
- Object O1 = ElementAt (right);
- int i = left- 1;
- int j = right;
- While (true) {
- While (Compare.lessthan (
- ElementAt (++i), O1))
- ;
- While (J > 0)
- if (compare.lessthanorequal (
- ElementAt (--j), O1))
- Break ;
- if (i >= j) break ;
- Swap (I, j);
- }
- Swap (I, right);
- QuickSort (left, I-1);
- QuickSort (i+1, right);
- }
- }
- private void swap (int loc1, int loc2) {
- Object tmp = ElementAt (LOC1);
- Setelementat (ElementAt (LOC2), Loc1);
- Setelementat (TMP, LOC2);
- }
- }
callback function Learning in Java