如何在 intellij 中快速重构 - 移动实例方法

How to refactor fast in intellij - move instance method

Intellij 中最快 将java 实例方法重构为另一个class 的方法是什么(重构)步骤。

例如:

public class Bar {
    public String getX(){
        return "x";
    }
}

public class Foo {
}

public class Runner {
    public static void main(String[] args) {
        new Bar().getX();
    }
}

重构为:

public class Bar {
}

public class Foo{
    public String getX(){
        return "x";
    }
}
public class Runner {
    public static void main(String[] args) {
        new Foo().getX();
    }
}

您可以使用 Shift+F6 重命名 class/method/variable(只需 select 名称并按热键),但您应该知道它不会删除或更改已经存在的名称,因此您会得到一个副本。最好的选择是在重命名 Bar class.

之前删除 Foo class

在 IntellJ IDEA 15 中你可以做:

  1. 方法的位置getX();
  2. 按 Ctrl/Cmd+F6(更改签名)
  3. 添加参数:类型=Foo,名称=foo,默认值=new Foo()
  4. 按 F6:移动方法
  5. Select Foo foo 作为目标并按 Refactor