意图
- 使得同一个类中的所有函数都可以获得这份信息,能够为这个类编写更清晰的代码
示例
/** * 以查询取代临时变量之前 * Created by luo on 2017/4/19. */ public class ReplaceTempWithQueryBefore { private double _quantity; private double _itemPrice; public double test() { double basePrice = _quantity * _itemPrice; if (basePrice > 1000) { return basePrice * 0.95; } else { return basePrice * 0.98; } } } /** * 以查询取代临时变量之后 * Created by luo on 2017/4/19. */ public class ReplaceTempWithQueryAfter { private double _quantity; private double _itemPrice; public double test() { if (basePrice() > 1000) { return basePrice() * 0.95; } else { return basePrice() * 0.98; } } private double basePrice() { return _quantity * _itemPrice; } }
时间: 2024-10-14 12:07:32