A Refactoring Example

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Consider of the following code segment:

class Apple {

private Boolean mature;

public void setMature(Season season) {

switch (season) {

...

}

}

};

Firstly, “mature“ is not a correct English word for “Apple”. Normally, we say “An apple is riple other than mature.

Secondly, the method “setMature()”is NOT setting mature. Normally, it is better to give funciton parameters the same name as the member variables you are assigning them to. Therefore, change the member variable to “season”, then change the member function to “setSeason()” and add another member function, namely “isMature()”, to the class.

At last, regarding the season in which an apple is riple may be different from countries to countries, change “isMature()“ to be virtual, so that the sub classes can override it to provide another implementation.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航