锁定老帖子 主题:请问这段代码如何重构
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-08-30
public class Main { public List init() { List list = new ArrayList(); list.add(new Student("张三",13)); list.add(new Student("是三",12)); list.add(new Student("句三",14)); list.add(new Student("李三",18)); list.add(new Student("飞三",11)); list.add(new Student("也三",17)); list.add(new Student("去三",15)); list.add(new Student("提三",12)); list.add(new Student("李三",13)); list.add(new Student("人三",18)); list.add(new Student("他三",13)); list.add(new Student("米三",15)); list.add(new Student("平三",16)); return list; } public int getTotalAge(List list) { Student student = null; int age = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); age = age + student.getAge(); } return age; } public int getNum(List list) { Student student = null; int num = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); if(student.getName().startsWith("李")) num ++; } return num; } public static void main(String args[]) { Main main = new Main(); List list = main.init(); System.out.println(main.getTotalAge(list)); System.out.println(main.getNum(list)); } } 上面的代码中,有2个方法的for循环部分重复。如何消除重复的代码。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-08-30
这段代码我觉得没必要重构,如果整合起来反而会影响对业务逻辑的理解,比如如此:
public int computer(List list, int type){ ... int ret = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); switch(type){ case 0: ret += student.getAge(); break; case 1: if(student.getName().startsWith("李")) ret ++; break; } return ret; } |
|
返回顶楼 | |
发表时间:2007-08-30
这种for不需要重构,要么将 java 代码
增加一个参数,改为 java 代码
|
|
返回顶楼 | |
发表时间:2007-08-30
这段代码确实没有必要重构,他逻辑上也不复杂,for循环的重复也是很常见的,如果lz硬是要重构,可以这样
java 代码
这样确实只有一个for语句,不过对于这种简单的逻辑,是不是有点过度了,程序只要运行正确,便于维护,开发快速就可以了,多一个for少一个for,就lz的例子来说,性能上的差异几乎可以忽略
|
|
返回顶楼 | |
发表时间:2007-08-30
如果那“张三,李三”等一共是10w个兄弟姐妹的时候,我想还是需要重构一下的,一次循环,传回一个List<int ,int>, 但是现在计划生育抓的好,他们一共才那么几个兄弟姐妹,for他10次也不慢哇
|
|
返回顶楼 | |
发表时间:2007-08-30
我记得ajoo以前有个帖子和你的问题很像,思路是抽出一个Getter接口,在Getter接口的方法中传入数据对象,思路和pheh的有些类似,你可以在je里搜索一下。
|
|
返回顶楼 | |
发表时间:2007-08-30
import java.util.ArrayList; import java.util.List; public class Main { public List init() { List list = new ArrayList(); list.add(new Student("张三",13)); list.add(new Student("是三",12)); list.add(new Student("句三",14)); list.add(new Student("李三",18)); list.add(new Student("飞三",11)); list.add(new Student("也三",17)); list.add(new Student("去三",15)); list.add(new Student("提三",12)); list.add(new Student("李三",13)); list.add(new Student("人三",18)); list.add(new Student("他三",13)); list.add(new Student("米三",15)); list.add(new Student("平三",16)); return list; } public TotalResult getTotalResult(List list){ Student student = null; int age = 0; int num = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); age = age + student.getAge(); if(student.getName().startsWith("李")) num ++; } return new TotalResult(age,num); } public int getTotalAge(List list) { Student student = null; int age = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); age = age + student.getAge(); } return age; } public int getNum(List list) { Student student = null; int num = 0; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); if(student.getName().startsWith("李")) num ++; } return num; } public static void main(String args[]) { Main main = new Main(); List list = main.init(); // System.out.println(main.getTotalAge(list)); // System.out.println(main.getNum(list)); System.out.println(main.getTotalResult(list)); } } class Student{ private String name; private int age; public Student(){ } public Student(String name,int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class TotalResult{ private int age; private int num; public TotalResult(int age,int num){ this.age = age; this.num = num; } public String toString(){ return "age:"+age+",num:"+num; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } |
|
返回顶楼 | |
发表时间:2007-09-11
import java.util.ArrayList;
import java.util.List; public class Main { public List init() { List students = new ArrayList(); students.add(new Student("张三",13)); students.add(new Student("是三",12)); students.add(new Student("句三",14)); students.add(new Student("李三",18)); students.add(new Student("飞三",11)); students.add(new Student("也三",17)); students.add(new Student("去三",15)); students.add(new Student("提三",12)); students.add(new Student("李三",13)); students.add(new Student("人三",18)); students.add(new Student("他三",13)); students.add(new Student("米三",15)); students.add(new Student("平三",16)); return students; } public static void main(String args[]) { Main m = new Main(); Student first = new Student("new one",12); Student second = new Student("new second",23); List list = m.init(); ManagerStudent ms = new ManagerStudent(list); ms.addStudent(first); ms.addStudent(second); ms.removeStudent(first); System.out.println(ms.getTotalAge()); System.out.println(ms.getNum()); } } class Student{ private String name; private int age; public Student(){ } public Student(String name,int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } class ManagerStudent{ private Student student; private List students = new ArrayList(); public ManagerStudent(List list){ students = list; } public int getTotalAge() { Student student = null; int age = 0; for(int i = 0; i < students.size(); i++) { student = (Student)students.get(i); age = age + student.getAge(); } return age; } public int getNum() { Student student = null; int num = 0; for(int i = 0; i < students.size(); i++) { student = (Student)students.get(i); if(student.getName().startsWith("李")) num ++; } return num; } public List getStudents() { return students; } public void setStudents(List students) { this.students.addAll(students); } public void addStudent(Student one){ students.add(one); } public void removeStudent(Student one){ if(students.contains(one)){ students.remove(one); }else{ System.err.println("不包含此学生!"); return; } } } |
|
返回顶楼 | |
发表时间:2007-09-11
public class Main {
public int num = 0; public int age = 0; public List init() { List list = new ArrayList(); list.add(new Student("张三",13)); list.add(new Student("是三",12)); list.add(new Student("句三",14)); list.add(new Student("李三",18)); list.add(new Student("飞三",11)); list.add(new Student("也三",17)); list.add(new Student("去三",15)); list.add(new Student("提三",12)); list.add(new Student("李三",13)); list.add(new Student("人三",18)); list.add(new Student("他三",13)); list.add(new Student("米三",15)); list.add(new Student("平三",16)); return list; } public void getTotalAge(List list) { Student student = null; for(int i = 0; i < list.size(); i++) { student = (Student)list.get(i); if(student.getName().startsWith("李")) num ++; age = age + student.getAge(); } } public static void main(String args[]) { Main main = new Main(); List list = main.init(); System.out.println(age); System.out.println(num); } } |
|
返回顶楼 | |
发表时间:2007-09-11
lz有点过分追求了吧。。。
|
|
返回顶楼 | |