<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    和風細雨

    世上本無難事,心以為難,斯乃真難。茍不存一難之見于心,則運用之術自出。

    用匿名類處理分類匯總的方法

    分類匯總是統計中常用,舉例來說如統計學生成績,及格不及格的歸類,分優良中差等級歸類等,每個單項代碼很好寫,但是如果分類匯總的項目多了,能一種匯總寫一個函數嗎? 比如說有些科目60分才算及格,有些科目50分就算;有些老師喜歡分優良中差四等,有些老師卻喜歡分ABCD;不一而足,如果每個都寫一個函數無疑是個編寫和維護惡夢. 如果我們用匿名類把分類匯總的規則分類匯總的過程分別抽象出來,代碼就清晰靈活多了,以下代碼講述了這個過程,代碼比較簡單,這里就不贅述了,相信大家都能看明白.

    代碼下載:
    http://m.tkk7.com/Files/sitinspring/ClassSummary20070928113810.rar

    首先是數據的基本類Student:
    public class Student{
        
    private String name;
        
    private int score;
        
        
    public Student(String name,int score){
            
    this.name=name;
            
    this.score=score;
        }

        
        
    public String getName() {
            
    return name;
        }

        
    public void setName(String name) {
            
    this.name = name;
        }

        
    public int getScore() {
            
    return score;
        }

        
    public void setScore(int score) {
            
    this.score = score;
        }
        
    }

    然后是用于分類匯總的類,它強制子類實現getKey和getvalue兩個方法:
    public abstract class ClassifyRule {
        
    public Student student;
        
        
    public ClassifyRule(){        
        }
        

        
    public void setStudent(Student student) {
            
    this.student = student;
        }

        
        
    abstract public String getKey();
        
    abstract public int getValue();
    }

    接下來是對Student進行CRUD處理的StudentService類,注意getSum方法,它保留了篩選過程,篩選規則則不在其中:
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.List;

    public class StudentService {
        
    private List<Student> students;

        
    public StudentService() {
            students 
    = new ArrayList<Student>();
        }


        
    public void add(Student student) {
            students.add(student);
        }


        
    public Hashtable<String, Integer> getSum(ClassifyRule rule) {
            Hashtable
    <String, Integer> ht = new Hashtable<String, Integer>();

            
    for (Student student : students) {
                rule.setStudent(student);
                String key 
    = rule.getKey();
                
    int value = rule.getValue();

                
    if (ht.containsKey(key)) {
                    Integer oldValue 
    = ht.remove(key);
                    oldValue 
    += value;
                    ht.put(key, oldValue);
                }
     else {
                    ht.put(key, value);
                }

            }


            
    return ht;
        }

    }

    最后是測試代碼,注意其中篩選規則的創建:
    import java.util.Hashtable;
    import java.util.Iterator;

    public class Test {
        
    public static void main(String[] args) {
            
    // 初始化
            StudentService service = new StudentService();
            service.add(
    new Student("Andy"90));
            service.add(
    new Student("Bill"95));
            service.add(
    new Student("Cindy"70));
            service.add(
    new Student("Dural"85));
            service.add(
    new Student("Edin"60));
            service.add(
    new Student("Felix"55));
            service.add(
    new Student("Green"15));

            
    // 60分及格篩選
            ClassifyRule rule60 = new ClassifyRule() {
                
    public String getKey() {
                    
    return student.getScore() >= 60 ? "及格" : "不及格";
                }


                
    public int getValue() {
                    
    return 1;
                }

            }
    ;

            System.out.println(
    "60分及格篩選");
            printHt(service.getSum(rule60));

            
    // 50分及格篩選
            ClassifyRule rule50 = new ClassifyRule() {
                
    public String getKey() {
                    
    return student.getScore() >= 50 ? "及格" : "不及格";
                }


                
    public int getValue() {
                    
    return 1;
                }

            }
    ;

            System.out.println(
    "\n50分及格篩選");
            printHt(service.getSum(rule50));

            
    // 分"優良中差"等級
            ClassifyRule ruleCn = new ClassifyRule() {
                
    public String getKey() {
                    String retval 
    = "";

                    
    int score = student.getScore();
                    
    if (score >= 90{
                        retval 
    = "";
                    }
     else if (score >= 80{
                        retval 
    = "";
                    }
     else if (score >= 60{
                        retval 
    = "";
                    }
     else if (score > 0{
                        retval 
    = "";
                    }


                    
    return retval;
                }


                
    public int getValue() {
                    
    return 1;
                }

            }
    ;

            System.out.println(
    "\n分優良中差等級篩選");
            printHt(service.getSum(ruleCn));

            
    // 分"ABCD"等級
            ClassifyRule ruleWest = new ClassifyRule() {
                
    public String getKey() {
                    String retval 
    = "";

                    
    int score = student.getScore();
                    
    if (score >= 90{
                        retval 
    = "A";
                    }
     else if (score >= 80{
                        retval 
    = "B";
                    }
     else if (score >= 60{
                        retval 
    = "C";
                    }
     else if (score > 0{
                        retval 
    = "D";
                    }


                    
    return retval;
                }


                
    public int getValue() {
                    
    return 1;
                }

            }
    ;

            System.out.println(
    "\n分ABCD等級篩選");
            printHt(service.getSum(ruleWest));
        }


        
    private static void printHt(Hashtable ht) {
            
    for (Iterator it = ht.keySet().iterator(); it.hasNext();) {
                String key 
    = (String) it.next();
                Integer value 
    = (Integer) ht.get(key);
                System.out.println(
    "Key=" + key + " Value=" + value);
            }

        }

    }

    測試結果如下:

     

    60分及格篩選
    Key
    =及格 Value=5
    Key
    =不及格 Value=2

    50分及格篩選
    Key
    =及格 Value=6
    Key
    =不及格 Value=1

    分優良中差等級篩選
    Key
    =優 Value=2
    Key
    =良 Value=1
    Key
    =中 Value=2
    Key
    =差 Value=2

    分ABCD等級篩選
    Key
    =A Value=2
    Key
    =D Value=2
    Key
    =C Value=2
    Key
    =B Value=1

    原理不復雜,這個抽象的過程還是有點意思的.

    posted on 2008-02-22 12:15 和風細雨 閱讀(391) 評論(0)  編輯  收藏 所屬分類: 算法

    主站蜘蛛池模板: 无遮挡国产高潮视频免费观看| 亚洲色精品VR一区区三区 | 亚洲人成网站18禁止久久影院| 免费播放一区二区三区| 亚洲国产精品第一区二区| 日本黄色动图免费在线观看| 久久久久亚洲av无码专区蜜芽| 免费看搞黄视频网站| 亚洲欧洲日产专区| 免费电视剧在线观看| 色综合久久精品亚洲国产| 四虎影在线永久免费观看| 一区二区免费电影| 亚洲精品国精品久久99热一| 日韩精品久久久久久免费| 亚洲AV无码乱码在线观看代蜜桃 | 亚洲视频在线免费看| 亚洲ts人妖网站| 好爽好紧好大的免费视频国产| 成人一级免费视频| 亚洲国产三级在线观看| 亚洲免费福利视频| 看Aⅴ免费毛片手机播放| 亚洲日韩欧洲无码av夜夜摸| 四虎国产精品永久免费网址| 免费一级做a爰片久久毛片潮喷| 无码毛片一区二区三区视频免费播放 | 亚洲Av无码国产情品久久 | 中文字字幕在线高清免费电影| 亚洲成人午夜在线| 性色av无码免费一区二区三区| 一级黄色片免费观看| 亚洲人成网站影音先锋播放| 亚洲精品动漫免费二区| 又硬又粗又长又爽免费看 | 国产免费怕怕免费视频观看| 国产特黄特色的大片观看免费视频| 亚洲最新黄色网址| 亚洲AV无码乱码精品国产| 中文字幕天天躁日日躁狠狠躁免费| 亚洲精品伦理熟女国产一区二区 |