java基础之:匿名内部类应用例子一
一:接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.yeepay.sxf.testclassforinner; 2 3 /** 4 * 回调接口. 5 * @author shangxiaofei 6 * 7 */ 8 public interface CallBack { 9 10 public boolean process(Integer a); 11 12 public boolean delete(Integer b); 13 }
二:业务类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.yeepay.sxf.testclassforinner; 2 /** 3 * 业务类使用回调接口 4 * @author shangxiaofei 5 * 6 */ 7 public class PayMentService { 8 9 10 public String payforMoney(Integer a,CallBack callBack){ 11 System.out.println("PayMentService.payforMoney()"+a); 12 Integer m=a+100; 13 if(m>100){ 14 boolean falg=callBack.process(m); 15 if(falg){ 16 return "callBack.process"+m; 17 }else{ 18 return "callBack.process"+m; 19 } 20 21 }else{ 22 boolean flag=callBack.delete(m); 23 if(flag){ 24 return "callBack.delete"+m; 25 }else{ 26 return "callBack.delete"+m; 27 } 28 } 29 } 30 }
三:控制类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.yeepay.sxf.testclassforinner; 2 /** 3 * 测试类 4 * @author shangxiaofei 5 * 6 */ 7 public class TestController { 8 9 private PayMentService payMentService=new PayMentService(); 10 11 private String dd="sxf"; 12 13 public void testPay(){ 14 15 String aString=payMentService.payforMoney(100, new CallBack() { 16 17 @Override 18 public boolean process(Integer a) { 19 String mString=""; 20 if(a>100){ 21 mString="sxf"; 22 } 23 if(mString.equals(dd)){ 24 return true; 25 } 26 return false; 27 } 28 29 @Override 30 public boolean delete(Integer b) { 31 String mString=""; 32 if(b<100){ 33 mString="sxf"; 34 } 35 if(mString.equals(dd)){ 36 return true; 37 } 38 return false; 39 } 40 }); 41 42 43 System.out.println("enclosing_type.enclosing_method()"+aString); 44 } 45 46 47 public static void main(String[] args) { 48 TestController controller=new TestController(); 49 controller.testPay(); 50 } 51 }