首页 用匿名类处理分类汇总的方法

用匿名类处理分类汇总的方法

举报
开通vip

用匿名类处理分类汇总的方法用匿名类处理分类汇总的方法 分类汇总是统计中常用,举例来说如统计学生成绩,及格不及格的归类,分优良中差等级归类等,每个单项代码很好写,但是如果分类汇总的项目多了,能一种汇总写一个函数吗?比如说有些科目60分才算及格,有些科目50分就算;有些老师喜欢分优良中差四 等,有些老师却喜欢分ABCD;不一而足,如果每个都写一个函数无 疑是个编写和维护恶梦.如果我们用匿名类把分类汇总的规则和 分类汇总的过程分别抽象出来,代码就清晰灵活多了,以下代码讲述了这个过程,代码比较简单,这里就不赘述了,相信大家都能看明白. 首先是数...

用匿名类处理分类汇总的方法
用匿名类处理分类汇总的 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 分类汇总是统计中常用,举例来说如统计学生成绩,及格不及格的归类,分优良中差等级归类等,每个单项代码很好写,但是如果分类汇总的项目多了,能一种汇总写一个函数吗?比如说有些科目60分才算及格,有些科目50分就算;有些老师喜欢分优良中差四 等,有些老师却喜欢分ABCD;不一而足,如果每个都写一个函数无 疑是个编写和维护恶梦.如果我们用匿名类把分类汇总的规则和 分类汇总的过程分别抽象出来,代码就清晰灵活多了,以下代码讲述了这个过程,代码比较简单,这里就不赘述了,相信大家都能看明白. 首先是数据的基本类Student: publicclassStudent{ privateStringname; privateintscore; publicStudent(Stringname,intscore){ this.name=name; this.score=score; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetScore(){ returnscore; } publicvoidsetScore(intscore){ this.score=score; } } 然后是用于分类汇总的类,它强制子类实现getKey和getval ue两个方法: publicabstractclassClassifyRule{ publicStudentstudent; publicClassifyRule(){ } publicvoidsetStudent(Studentstudent){ this.student=student; } abstractpublicStringgetKey(); abstractpublicintgetValue(); } 接下来是对Student进行CRUD处理的StudentService类, 注意getSum方法,它保留了筛选过程,筛选规则则不在其中: importjava.util.ArrayList; importjava.util.Hashtable; importjava.util.List; publicclassStudentService{ privateListstudents; publicStudentService(){ students=newArrayList(); } publicvoidadd(Studentstudent){ students.add(student); } publicHashtablegetSum(ClassifyRulerule){ Hashtableht=newHashtable(); for(Studentstudent:students){ rule.setStudent(student); Stringkey=rule.getKey(); intvalue=rule.getValue(); if(ht.containsKey(key)){ IntegeroldValue=ht.remove(key); oldValue+=value; ht.put(key,oldValue); }else{ ht.put(key,value); } } returnht; } } 最后是测试代码,注意其中筛选规则的创建: importjava.util.Hashtable; importjava.util.Iterator; publicclassTest{ publicstaticvoidmain(String[]args){ //初始化 StudentServiceservice=newStudentService(); service.add(newStudent("Andy",90)); service.add(newStudent("Bill",95)); service.add(newStudent("Cindy",70)); service.add(newStudent("Dural",85)); service.add(newStudent("Edin",60)); service.add(newStudent("Felix",55)); service.add(newStudent("Green",15)); //60分及格筛选 ClassifyRulerule60=newClassifyRule(){ publicStringgetKey(){ returnstudent.getScore()>=60?"及格":"不及格"; } publicintgetValue(){ return1; } }; System.out.println("60分及格筛选"); printHt(service.getSum(rule60)); //50分及格筛选 ClassifyRulerule50=newClassifyRule(){ publicStringgetKey(){ returnstudent.getScore()>=50?"及格":"不及格"; } publicintgetValue(){ return1; } }; System.out.println("\n50分及格筛选"); printHt(service.getSum(rule50)); //分"优良中差"等级 ClassifyRuleruleCn=newClassifyRule(){ publicStringgetKey(){ Stringretval=""; intscore=student.getScore(); if(score>=90){ retval="优"; }elseif(score>=80){ retval="良"; }elseif(score>=60){ retval="中"; }elseif(score>0){ retval="差"; } returnretval; } publicintgetValue(){ return1; } }; System.out.println("\n分优良中差等级筛选"); printHt(service.getSum(ruleCn)); //分"ABCD"等级 ClassifyRuleruleWest=newClassifyRule(){ publicStringgetKey(){ Stringretval=""; intscore=student.getScore(); if(score>=90){ retval="A"; }elseif(score>=80){ retval="B"; }elseif(score>=60){ retval="C"; }elseif(score>0){ retval="D"; } returnretval; } publicintgetValue(){ return1; } }; System.out.println("\n分ABCD等级筛选"); printHt(service.getSum(ruleWest)); } privatestaticvoidprintHt(Hashtableht){ for(Iteratorit=ht.keySet().iterator();it.hasNext();){ Stringkey=(String)it.next(); Integervalue=(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=AValue=2 Key=DValue=2 Key=CValue=2 Key=BValue=1
本文档为【用匿名类处理分类汇总的方法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_998870
暂无简介~
格式:doc
大小:22KB
软件:Word
页数:0
分类:互联网
上传时间:2017-10-25
浏览量:27