Class A holds the object reference of the B interface, the B interface has a callback () method, the Class C is an implementation class of Class B, implements the callback () method, passes the class C into Class A, and when class A executes the operation calls the callback () method, then a calls the Class C callback ( ) method, which is equivalent to notifying C "My operation is complete," we just write the callback () method in Class C, which is the callback. Understanding is not difficult, write more can be!
1 Public InterfaceB {2 Public voidcllback (String result);3 }4 Public classCImplementsb{5 6 Public voidcllback (String result) {7 //TODO auto-generated Method Stub8 if(Result.equals ("Operation completed")) {9System.out.println ("I am c, received a notice, A has finished work");Ten } One } A - } - Public classA { the B OBJECTB; - Public voidSETOBJECTB (B objectb) { - This. OBJECTB =OBJECTB; - } + Public voidoperation () { - /* + * Business Operations A * Business Operations at * */ -Objectb.cllback ("Operation Complete"); - } - } - Public classMain { - Public Static voidMain (string[] args) { in //TODO auto-generated Method Stub -A Objecta=NewA (); toOBJECTA.SETOBJECTB (NewC ()); + objecta.operation (); - } the *}
Understanding of the Java callback interface