修改前:
get price(){
double basePrice = _quantity * _itemPrice;
if( basePrice > 1000){
return basePrice*0.8
}else{
return basePrice*0.98
}
}
修改后:
getPrice(){
return basePrice()* discountFactor();
}
double basePrice(){
return _quantity * _itemPrice;
}
double discountFactor(){
if(basePrice()>1000){
return 0.8;
}else{
rerun 0.98;
}
}
注: 修改前 先用final 测试 变量是否被多次修改
final basePrice = _quantity * _itemPrice;