【1】原代码
1 if (date.before(SUMMER_START) || date.after(SUMMER_END)) 2 { 3 charge = quantity * _winterRate + _winterServiceCharge; 4 } 5 else 6 { 7 charge = quantity * _summerRate; 8 }
【2】分解条件式
1 if (notSummer(date)) 2 { 3 charge = winterCharge(quantity); 4 } 5 else 6 { 7 charge = summerCharge (quantity); 8 } 9 10 double summerCharge(int quantity) 11 { 12 return quantity * _summerRate; 13 } 14 15 double winterCharge(int quantity) 16 { 17 return quantity * _winterRate + _winterServiceCharge; 18 }
【3】总结
有一个复杂的条件(if-then-else)语句。从if、then、else三个段落中分别提炼出独立函数。
Good Good Study, Day Day Up.
顺序 选择 循环 总结