| 网站首页 | 行业资讯 | 操作系统 | 网络技术 | 网页设计 | 平面设计 | 媒体制作 | 编程开发 | 
 | 办公软件 | 网络安全 | 电脑技术 | 数 据 库 | 课程在线 | 成功案例 | 项目合作 | 团队简介 | 
蓝色极速感谢大家的支持!站长:干戈 QQ:495979847     IAS网上答疑系统V1.0发布  [Sunpeople  2007年9月15日]        
  您现在的位置: 蓝色极速 >> 编程开发 >> Java >> 教程正文
 

没有任何图片教程

        ★★★ 
Eclipse中自动重构实现探索
作者:未知    教程来源:网络    点击数:    更新时间:2007-5-12    
e
  return 1;
  }

  2、 把getFrequentRenterPoints()移动到Rental类中。

  3、 对getFrequentRenterPoints()"更改方法特征符"为public。

  4、 对Customer的函数getFrequentRenterPoints()执行内联操作,重构目标完成。

  五、重构第四步:去除临时变量(totalAmount和frequentRenterPoints)

  目的:去除临时变量(totalAmount和frequentRenterPoints)

  方法:

  1、 分析totalAmount和frequentRenterPoints的定义和引用结构如下:

  // 声明和定义
  double totalAmount = 0;
  int frequentRenterPoints = 0;
  ……
  // 在循环中修改
  while(rentals.hasMoreElements()){
  ……
  frequentRenterPoints += each.getFrequentRenterPoints();
  ……
  totalAmount += each.getCharge();
  ……
  }
  ……
  // 在循环外使用
  result += "Amount owed is " + String.valueOf(totalAmount) + " ";
  result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
  ……

  上述两个变量在循环体外面定义和使用,在循环中被修改,运用Replace Temp with Query方法去除这两个临时变量是一项稍微复杂的重构。很遗憾,eclipse目前不支持这样的重构。

  2、手工修改代码。

  六、重构第五步:运用多态取代与价格相关的条件逻辑

  目的:

  1、 把Rental类中的函数getCharge()移动到Movie类中。

  2、 把Rental类中的函数getFrequentRenterPoints()移动到Movie类中。

  重构方法:

  Move Method
  Inline Method

  方法:

  1、 选中Rental类中的函数getCharge(),右键菜单选中"重构/移动",eclipse提示找不到接收者,不能移动。原因在于这行语句:

  switch(getMovie().getPriceCode()){//取得影片出租价格

  选中getMovie(),右键菜单选中"重构/内联",确定后代码成为:

  switch(_movie.getPriceCode()){ //取得影片出租价格

  选中getCharge(),执行"重构/移动"后,函数被移动到Movie类中。然而这只是部分达成了重构目的,我们发现,移动后的代码把Rental作为参数传给了getCharge(),手工修改一下,代码变成:

  class Movie ……
  /**
  * @param this
  * @return
  */
  public double getCharge(int _daysRented) {
  double result = 0;
  switch(getPriceCode()){ //取得影片出租价格
  case Movie.REGULAR: // 普通片
   result += 2;
   if(_daysRented>2)
    result += (_daysRented-2)*1.5;
   break;

  case Movie.NEW_RELEASE: // 新片
   result += _daysRented*3;
   break;

  case Movie.CHILDRENS: // 儿童片
   result += 1.5;
   if(_daysRented>3)
    result += (_daysRented-3)*1.5;
   break;
  }
  return result;
  }

  class Rental……
  /**
  * @param this
  * @return
  */
  public double getCharge() {
  return _movie.getCharge(_daysRented);
  }

  2、用同样的步骤处理getFrequentRenterPoints(),重构后的代码:

  class Movie ……
  /**
  * @param frequentRenterPoints
  * @param this
  * @return
  */
  public int getFrequentRenterPoints(int daysRented) {
  if((getPriceCode())==Movie.NEW_RELEASE && daysRented>1)
  return 2;
  else
  return 1;
  }
  class Rental……
  /**
  * @param frequentRenterPoints
  * @param this
  * @return
  */
  public int getFrequentRenterPoints(int daysRented) {
  if((getPriceCode())==Movie.NEW_RELEASE && daysRented>1)
  return 2;
  else

上一页  [1] [2] [3] [4] [5] 下一页

教程录入:Sunpeople    责任编辑:Sunpeople  
  • 上一个教程:

  • 下一个教程: 没有了

  •      
    点击申请点击申请点击申请点击申请点击申请点击申请点击申请
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 |

    Copyright(C)2006-2009 Exploit Team All Rights Reserved
    QQ:495979847 网络备案号:陇ICP备07001585号