理解:和提升字段正好相反,跟降低方法类似,就是把基类中,只有部分继承类需要用到的字段,降低到继承类自身去。
详解:
重构前代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace _31DaysRefactor 7 { 8 public abstract class Task 9 { 10 protected string _resolution; 11 } 12 13 public class BugTask : Task 14 { 15 16 } 17 18 public class FeatureTask : Task 19 { 20 21 } 22 }
重构后代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace _31DaysRefactor 7 { 8 public abstract class Task 9 { 10 protected string _resolution; 11 } 12 13 public class BugTask : Task 14 { 15 private string _resolution; 16 } 17 18 public class FeatureTask : Task 19 { 20 21 } 22 }
把 _resolution字段降低到继承类 BugTask中去。