我该如何应用“移动方法”?使用IntelliJ IDEA进行重构? [英] How can I apply the "move method" refactoring with IntelliJ IDEA?

查看:887
本文介绍了我该如何应用“移动方法”?使用IntelliJ IDEA进行重构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的代码示例:

public class TheClass {

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

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

public class OtherClass {

}

假设我想将 theMethod TheClass 移至 OtherClass 。在IDEA中是否有自动重构,如果是这样的话:我如何应用它?

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?

推荐答案

移动方法重构IDEA仅考虑将方法移动到与其相关的类中,即用作其参数或返回值,或从方法内部调用。这有点合乎逻辑:如果方法与目标类没有任何关系,为什么它应该存在呢? 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.

这篇关于我该如何应用“移动方法”?使用IntelliJ IDEA进行重构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆