重构

  1. package com.actmaps;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6.   
  7. /**  
  8.  * TODO   
  9.  * @author PHeHPHeH
     
  10.  * Created On 2007-8-30 16:04:27  
  11.  */  
  12. public class Test {   
  13.               
  14.     public List init() {      
  15.         List list = new ArrayList();      
  16.         list.add(new Student("张三",13));      
  17.         list.add(new Student("是三",12));      
  18.         list.add(new Student("句三",14));      
  19.         list.add(new Student("李三",18));      
  20.         list.add(new Student("飞三",11));      
  21.         list.add(new Student("也三",17));      
  22.         list.add(new Student("去三",15));      
  23.         list.add(new Student("提三",12));      
  24.         list.add(new Student("李三",13));      
  25.         list.add(new Student("人三",18));      
  26.         list.add(new Student("他三",13));      
  27.         list.add(new Student("米三",15));      
  28.         list.add(new Student("平三",16));      
  29.               
  30.         return list;      
  31.     }   
  32.        
  33.     public int iterList(List list,IDoExecute tool){   
  34.         int result = 0;   
  35.         Student student = null;   
  36.         for(int i = 0; i < list.size(); i++) {      
  37.             student = (Student)list.get(i);      
  38.             result += tool.execute(student);   
  39.         }   
  40.         return result;   
  41.     }   
  42.           
  43.     public int getTotalAge(List list) {   
  44.         return this.iterList(list,new DoExecute(){   
  45.             public int execute(Student stu){   
  46.                 return stu.getAge();   
  47.             }   
  48.         });   
  49.     }      
  50.           
  51.     public int getNum(List list) {      
  52.         return this.iterList(list,new DoExecute(){   
  53.             public int execute(Student stu){   
  54.                 if(stu.getName().startsWith("李"))   
  55.                     return 1;   
  56.                 else  
  57.                     return 0;   
  58.             }   
  59.         });   
  60.     }      
  61.           
  62.     public static void main(String args[]) {   
  63.         Test test = new Test();   
  64.         List list = test.init();      
  65.         System.out.println(test.getTotalAge(list));      
  66.         System.out.println(test.getNum(list));      
  67.     }   
  68.        
  69.     class Student{   
  70.         private String name = "";   
  71.         private int age = 0;   
  72.         public Student(String n,int a){   
  73.             name = n;   
  74.             age = a;   
  75.         }   
  76.         public int getAge() {   
  77.             return age;   
  78.         }   
  79.         public void setAge(int age) {   
  80.             this.age = age;   
  81.         }   
  82.         public String getName() {   
  83.             return name;   
  84.         }   
  85.         public void setName(String name) {   
  86.             this.name = name;   
  87.         }   
  88.     }   
  89.     interface IDoExecute{   
  90.         public int execute(Student stu);   
  91.     }   
  92.     class DoExecute implements IDoExecute{   
  93.         public int execute(Student stu){   
  94.             return 0;   
  95.         }   
  96.     }   
  97. }     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值