如何应用“移动方法”使用 IntelliJ IDEA 进行重构?
我希望能够在 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 {
}
假设我想将 theMethod
从 TheClass
移动到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 IntelliJ 14-15 中,执行以下操作:
你最终会得到:
In IntelliJ 14-15 do the following:
You will end up with:
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.
在 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
如果 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.
还有另一种方法。假设您有以下代码:
并且您希望将
foo
设为静态。选择方法的整个主体并按 Alt+Ctrl+M(提取方法)。键入该方法的相同名称。选中“声明静态”复选框(仅当该方法仅读取且不修改字段时才可用)并按“确定”。所以你得到:将静态方法移动到任何你想要的地方并使用旧的 foo 的主体来调用它。
There is another method. Imagine you have the code:
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:Move static method wherever you want and use old foo's body to call it.