如何应用“移动方法”使用 IntelliJ IDEA 进行重构?

发布于 2024-10-21 09:54:08 字数 517 浏览 5 评论 0原文

我希望能够在 IntelliJ IDEA 中将实例方法从一个类移动到另一个类(Fowler 的“重构”中的“移动方法”)。不幸的是,当我尝试“移动...”(cmd:F6)时,它告诉我“没有具有引用类型的方法。您想让方法静态然后移动吗?”我不想使我的方法成为静态的,我希望它成为另一个类的实例方法。

我的代码示例:

public class TheClass {

  public void doStuff(){
     int i = themethod();
  }

  private int theMethod() {
    System.out.println( "Hello World!" );
    return 0;
  }
}

public class OtherClass {

}

假设我想将 theMethodTheClass 移动到 OtherClass。 IDEA 中是否有自动重构功能?如果有:我该如何应用它?

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..." (cmd: F6), it tells me that "There are no methods that have a reference type. Would you like to make method static and then move?" I do not want to make my method static, I want it to be an instance method on the other class instead.

My code example:

public class TheClass {

  public void doStuff(){
     int i = themethod();
  }

  private int theMethod() {
    System.out.println( "Hello World!" );
    return 0;
  }
}

public class OtherClass {

}

Say I want to move theMethod from TheClass to OtherClass. Is there an automatic refactoring in IDEA for this, and if so: How do I apply it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卖梦商人 2024-10-28 09:54:08

在 IntelliJ 14-15 中,执行以下操作:

  1. 将插入符号放在 theMethod() 上。
  2. 按 Ctrl/Cmd+F6(更改签名)。
  3. 引入新参数:Type=TheOtherClass,Name=theOtherClass,默认值=new TheOtherClass()
  4. Refactor
  5. 然后按F6(移动)将方法移动到theOtherClass。

你最终会得到:

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

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;
    }
}
内心激荡 2024-10-28 09:54:08

IDEA中的Move Method重构只考虑将方法移动到与其相关的类中,即用作其参数或返回值,或从方法内部调用。这有点合乎逻辑:如果该方法与目标类没有任何具体关系,为什么它应该在那里? OTOH 我发现在某些情况下存在这种限制,但我仍然有正当理由移动该方法。所以我必须手工完成。

The Move Method refactoring in IDEA only considers moving the method into classes related to it, i.e. used as its parameter or return value, or called from inside the method. Which is kinda logical: if the method has nothing concrete to do with the target class, why should it be there? OTOH I found this limiting in some cases where I still had a valid reason to move the method. So I had to do it by hand.

单身狗的梦 2024-10-28 09:54:08

在 intellij 13.1(不知道以前的版本)中,可以使用

Choose Refactor | 来完成摘录|在主菜单上进行委托

,但显然有一个“奇怪”的限制:它只能通过新创建的类来完成。
因此,您必须应用此重构而不创建“OtherClass”(它将在您应用重构时直接创建)。

因此,在已经创建的类上的方法的真正“移动”似乎缺失,这是相当奇怪的行为

In intellij 13.1 (dont' know in previous version) it could be done with the

Choose Refactor | Extract | Delegate on the main menu

but there is a "strange" limit, apparently: it could be done only with a new freshly created class.
So you have to do apply this refactoring without creating the "OtherClass" (it will be create directly when you apply the refactoring).

So a real "move" of method on an alredy created class seems missing, quite strange behaviou

楠木可依 2024-10-28 09:54:08

如果 theMethod() 没有对宿主类 (TheClass) 的任何引用,您可以将此方法设为静态,然后使用“移动”命令。将方法移至目标类后,应删除 static 关键字。

if theMethod() has nothing reference to the host class(TheClass), you can make this method static and then use "Move" command. After the method was moved to the target class, you should remove the static keyword.

柏拉图鍀咏恒 2024-10-28 09:54:08

还有另一种方法。假设您有以下代码:

public int field;

public void foo(int a) {
    assert field == a;
}

并且您希望将 foo 设为静态。选择方法的整个主体并按 Alt+Ctrl+M(提取方法)。键入该方法的相同名称。选中“声明静态”复选框(仅当该方法仅读取且不修改字段时才可用)并按“确定”。所以你得到:

public void foo(int a) {
    foo(a, field);
}

private static void foo(int a, int field) {
    assert field == a;
}

将静态方法移动到任何你想要的地方并使用旧的 foo 的主体来调用它。

There is another method. Imagine you have the code:

public int field;

public void foo(int a) {
    assert field == a;
}

And you want to make foo static. Select the whole body of the method and preess Alt+Ctrl+M (Extract method). Type the same name of the method. Check "Declare static" checkbox (available only if the method only reads and doesn't modify the fields) and press Ok. So you get:

public void foo(int a) {
    foo(a, field);
}

private static void foo(int a, int field) {
    assert field == a;
}

Move static method wherever you want and use old foo's body to call it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文