How can I apply the “move method” refactoring with IntelliJ IDEA?

前端 未结 5 754
感情败类
感情败类 2021-02-01 17:53

I want to be able to move an instance method from one class to another class (\"Move method\" from Fowler\'s \"Refactoring\") in IntelliJ IDEA. Unfortunately, when I try \"Move.

5条回答
  •  臣服心动
    2021-02-01 18:17

    In IntelliJ 14-15 do the following:

    1. Position the caret on theMethod().
    2. press Ctrl/Cmd+F6 (Change signature).
    3. Introduce new parameter: Type=TheOtherClass, Name=theOtherClass, Default value=new TheOtherClass()
    4. Refactor
    5. Then press F6 (move) and move the method to theOtherClass.

    You will end up with:

    public class TheClass {
        public void doStuff() {
            int i = new TheOtherClass().theMethod();
        }
    }
    public class TheOtherClass {
        int theMethod() {
            System.out.println("Hello World!");
            return 0;
        }
    }
    

提交回复
热议问题