【Eclipse】使用指南(12)提取方法

(翻译说明:http://my.oschina.net/ywlaker/blog/468911)

Extracting a new method

提取方法

In this section, you will improve the code of the constructor of junit.framework.TestSuite. To make the intent of the code clearer, you will extract the code that collects test cases from base classes into a new method called collectInheritedTests.    
本小节,你将改善junit.framework.TestSuite类的构造器代码。为了使代码目的更明确,你需要将基类中收集测试用例的代码提取为名为collectInheritedTests的方法。

  1. In the junit.framework/TestSuite.java file, select the following range of code inside the TestSuite(Class) constructor:      
    选择TestSuite类构造器中的如下代码:      

    Class superClass= theClass;        
    Vector names= new Vector();          
    while(Test.class.isAssignableFrom(superClass)) {          
      Method[] methods= superClass.getDeclaredMethods();          
      for (int i= 0; i < methods.length; i++) {          
        ddTestMethod(methods[i],names, theClass);          
      }          
      superClass= superClass.getSuperclass();          
    }

  2. From the selection's context menu in the editor, select Refactor > Extract Method....      
    在选中的范围内右键选择Refactor > Extract Method…菜单。      

    【Eclipse】使用指南(12)提取方法_第1张图片

  3. In the Method Name field, type collectInheritedTests.      
    在弹出对话框的Method Name栏输入collectInheritedTests      

    【Eclipse】使用指南(12)提取方法_第2张图片

  4. To preview the changes, press Preview>.The preview page displays the changes that will be made. Press OK to extract the method.      
    点击Preview>按钮预览更改,预览完,点击OK 按钮提取方法。      

    【Eclipse】使用指南(12)提取方法_第3张图片

  5. Go to the extracted method by selecting it in the Outline view.    
    在Outline视图中点击查看提取出来的方法。

你可能感兴趣的:(eclipse,method,refactor,extract)