今天的重构指南和“提升方法”有点类似,不过要提升的不是方法,而是字段,让我们来看下面的代码:
1: public abstract class Account
2: {
3: }
4:
5: public class CheckingAccount : Account
6: {
7: private decimal _minimumCheckingBalance = 5m;
8: }
9:
10: public class SavingsAccount : Account
11: {
12: private decimal _minimumSavingsBalance = 5m;
13: }
在上面的代码中,我们在每个子类里都有一个值为”5m”的字段,为了促进得用,我们将字段提升到基类中,并将提供一个简洁一些的名字。
1: public abstract class Account
2: {
3: protected decimal _minimumBalance = 5m;
4: }
5:
6: public class CheckingAccount : Account
7: {
8: }
9:
10: public class SavingsAccount : Account
11: {
12: }
原文链接:http://www.lostechies.com/blogs/sean_chambers/archive/2009/08/05/refactoring-day-5-pull-up-field.aspx