Eclipse,将一个java方法重构为另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9331352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse, refactoring a java method into a another class
提问by mmm
How can I refactor(move) a Java method in classA into classB and have all references to the method to it updated?
如何将 classA 中的 Java 方法重构(移动)到 classB 中并更新对该方法的所有引用?
Is this supported in Eclipse ?
这在 Eclipse 中受支持吗?
采纳答案by Thomas
For a static method, you can right click and select 'Move'.
对于静态方法,您可以右键单击并选择“移动”。
Obj1.myMethod()
would then get 'moved' to
然后会“移动”到
Obj2.myMethod()
and eclipse would fix your imports etc.
和 eclipse 会修复你的进口等。
For a non-static method, this may not work depending on the relationship between classA and classB.
对于非静态方法,根据 classA 和 classB 之间的关系,这可能不起作用。
Obj1 myobj1 = new Obj1();
myobj1.myMethod();
myobj1.myOtherMethod();
If you move myMethod() to a different class, the refactoring would have to change the object initialization. If myOtherMethod isn't getting moved, then it can't just change the type of myobj1 to Obj2 because then myOtherMethod won't work.
如果将 myMethod() 移动到不同的类,重构将不得不更改对象初始化。如果 myOtherMethod 没有被移动,那么它不能只是将 myobj1 的类型更改为 Obj2,因为这样 myOtherMethod 将不起作用。
回答by Deepak Azad
- Select the method in Outline view
- Refactor > Move
- 在大纲视图中选择方法
- 重构 > 移动
If you want to move the method to a new class - Refactor > Extract Class
如果要将方法移至新类 - 重构 > 提取类