One: interface
1 PackageCom.yeepay.sxf.testclassforinner;2 3 /**4 * Callback interface.5 * @authorShangxiaofei6 *7 */8 Public InterfaceCallBack {9 Ten Public Booleanprocess (Integer a); One A Public BooleanDelete (Integer b); -}
View Code
Second: Business class
1 PackageCom.yeepay.sxf.testclassforinner;2 /**3 * Business class using callback interface4 * @authorShangxiaofei5 *6 */7 Public classPaymentservice {8 9 Ten PublicString Payformoney (Integer a,callback CallBack) { OneSystem.out.println ("Paymentservice.payformoney ()" +a); AInteger m=a+100; - if(m>100){ - Booleanfalg=callback.process (m); the if(falg) { - return"Callback.process" +m; -}Else{ - return"Callback.process" +m; + } - +}Else{ A Booleanflag=Callback.delete (m); at if(flag) { - return"Callback.delete" +m; -}Else{ - return"Callback.delete" +m; - } - } in } -}
View Code
Three: Control class
1 PackageCom.yeepay.sxf.testclassforinner;2 /**3 * Test Class4 * @authorShangxiaofei5 *6 */7 Public classTestController {8 9 PrivatePaymentservice paymentservice=NewPaymentservice ();Ten One PrivateString dd= "SXF"; A - Public voidTestpay () { - theString Astring=paymentservice.payformoney (100,NewCallBack () { - - @Override - Public Booleanprocess (Integer a) { +String mstring= ""; - if(a>100){ +Mstring= "SXF"; A } at if(Mstring.equals (dd)) { - return true; - } - return false; - } - in @Override - Public BooleanDelete (Integer b) { toString mstring= ""; + if(b<100){ -Mstring= "SXF"; the } * if(Mstring.equals (dd)) { $ return true;Panax Notoginseng } - return false; the } + }); A the +System.out.println ("Enclosing_type.enclosing_method ()" +astring); - } $ $ - Public Static voidMain (string[] args) { -TestController controller=NewTestController (); the Controller.testpay (); - }Wuyi}
View Code
Java Basics: Anonymous Inner class application example one