Java 当两个类根本不相关时如何将方法从一个类移动到另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19995001/
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
How to move a method from a class to another class when two classes are not at all related
提问by Shruti Rawat
I am trying to re factor some code by breaking a class into several other classes. to do so i want to move some methods already existing in my old class to new class. But these methods are being referred in a lot of places and manually updating the references seems tiresome. So is there any way to move methods as well as update their references in eclipse?
我试图通过将一个类分成几个其他类来重新分解一些代码。为此,我想将旧类中已经存在的一些方法移动到新类中。但是这些方法在很多地方都被引用,手动更新引用似乎很烦人。那么有没有办法在eclipse中移动方法并更新它们的引用?
回答by PNS
If you use any of the standard IDEs (e.g., Eclipse
or IntelliJ IDEA
), they all have a simple menu option to do that (depending on how the code is organized).
如果您使用任何标准 IDE(例如,Eclipse
或IntelliJ IDEA
),它们都有一个简单的菜单选项来执行此操作(取决于代码的组织方式)。
If you go to each method and right-click on its name, the menu has a "Refactor" option, which leads to a "Move" option. Select that and follow the instructions.
如果您转到每个方法并右键单击其名称,则菜单中有一个“重构”选项,这会导致“移动”选项。选择它并按照说明进行操作。
The above is especially easy for static methods. For the non-static ones, you may need to do subclassing, or pass references to the appropriate objects around. Even so, the "Refactor -> Move" option is a good start.
以上对于静态方法尤其容易。对于非静态的,您可能需要进行子类化,或传递对适当对象的引用。尽管如此,“重构 -> 移动”选项是一个好的开始。
回答by Prabhakaran Ramaswamy
if you using eclipse IDE then refactor will help you.
如果您使用 Eclipse IDE,那么重构将对您有所帮助。
回答by René Link
I would do it this way:
我会这样做:
- Ensure that your tests work and the code to be re-factored is covered. If you don't have tests write tests. They are your safety rope.
- Use the re-factoring pattern extract superclassto create the new class that you want to move some methods to.
- Use the re-factoring pattern pull up methodsto move the methods along with the variables that they need to the superclass. Now you will see if the methods you want to move and the instance variables have dependencies to the other methods that you don't want to move. If so you must first break this dependencies.
- Find all client code that should use the new extracted class instead of the "old" class and rewrite it to the new extracted class.
- Remove the "extends" relationship between the two classes. Now the client code should work or you missed something.
- 确保您的测试有效,并且覆盖了要重构的代码。如果您没有测试,请编写测试。它们是您的安全绳。
- 使用重构模式提取超类来创建要将某些方法移动到的新类。
- 使用重构模式pull up 方法将方法连同它们需要的变量移动到超类。现在您将看到您想要移动的方法和实例变量是否与您不想移动的其他方法具有依赖关系。如果是这样,您必须首先打破这种依赖关系。
- 找到所有应该使用新提取的类而不是“旧”类的客户端代码,并将其重写为新提取的类。
- 删除两个类之间的“扩展”关系。现在客户端代码应该可以工作了,否则你错过了一些东西。
Also a good book for learning how to apply re-factoring patterns is Working Effectively with Legacy Code
学习如何应用重构模式的另一本好书是使用遗留代码有效工作
回答by Hussain Akhtar Wahid 'Ghouri'
does it have any of your satisfaction
有没有你满意的
package com.hussi.stackOverFlow;
class ClassOne {
public void methodInClassOne(String stringParam)
{
ClassTwo classTwoObj = new ClassTwo();
classTwoObj.methodInClassTwo(stringParam);
}
}
class ClassTwo {
public void methodInClassTwo(String stringParam)
{
System.out.println(stringParam);
}
}
public class ClassThree {
public static void main(String[] args)
{
ClassOne objClassOne = new ClassOne();
// calling method of class two in class one
objClassOne.methodInClassOne("pass this String value");
}
}
回答by Carl Manaster
- Copy the method into the new class.
- Replace the method body in the old class with a call into the new class.
- Inline the old method.
- 将方法复制到新类中。
- 将旧类中的方法体替换为对新类的调用。
- 内联旧方法。
That's all you need. It may not be quite that simple, because in steps 1 and 2 you may need to add arguments and/or make the method static, but that is the essence of how to do it.
这就是你所需要的。这可能不是那么简单,因为在步骤 1 和 2 中,您可能需要添加参数和/或使方法静态,但这就是如何做到这一点的本质。
回答by JnRouvignac
I will show you the process I follow. Consider such code:
我将向您展示我遵循的过程。考虑这样的代码:
public class GodClass {
public someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
public static void main(String[] args) {
GodClass c = ...;
c.someInstanceMethodToMove();
GodClass.someStaticMethodToMove();
}
}
Create the new class:
创建新类:
public class SingleResponsibilityClass {
}
The static method can be directly moved to the SingleResponsibilityClass
by using Eclipse's Refactor
> Move...
refactoring as described by Prabhakaran:
静态方法可以SingleResponsibilityClass
通过使用 Eclipse 的Refactor
>Move...
重构直接移动到,如 Prabhakaran 所述:
public class GodClass {
public someInstanceMethodToMove() {
// some code 1
}
public static void main(String[] args) {
GodClass c = ...;
c.someInstanceMethodToMove();
SingleResponsibilityClass.someStaticMethodToMove();
}
}
public class SingleResponsibilityClass {
public static someStaticMethodToMove() {
// some code 2
}
}
For the instance method, the process is a little more complex. Read below.
对于实例方法,过程稍微复杂一些。参见下文。
Extract a method out of someInstanceMethodToMove()
and let's name it someInstanceMethodToMove2()
:
从中提取一个方法someInstanceMethodToMove()
并命名为someInstanceMethodToMove2()
:
public class GodClass {
public someInstanceMethodToMove() {
someInstanceMethodToMove2();
}
private someInstanceMethodToMove2() {
// some code 1
}
// ...
}
Use the SingleResponsibilityClass
in the original method:
使用SingleResponsibilityClass
在原来的方法:
public class GodClass {
public someInstanceMethodToMove() {
someInstanceMethodToMove2(new SingleResponsibilityClass());
}
private someInstanceMethodToMove2(SingleResponsibilityClass obj) {
// some code 1
}
// ...
}
Note: it is important that SingleResponsibilityClass
is a parameter of the instance method to move, otherwise Eclipse will not move it to this type.
From there, right click on someInstanceMethodToMove2()
, and select Refactor
> Move...
, select SingleResponsibilityClass type in the wizard, then apply:
注意:重要的SingleResponsibilityClass
是要移动的实例方法的一个参数,否则Eclipse不会将其移动到这种类型。从那里,右键单击someInstanceMethodToMove2()
,然后选择Refactor
> Move...
,在向导中选择 SingleResponsibilityClass 类型,然后应用:
public class GodClass {
public someInstanceMethodToMove() {
new SingleResponsibilityClass().someInstanceMethodToMove2();
}
// ...
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove2() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}
Then right click on SingleResponsibilityClass
' someInstanceMethodToMove2()
method and Refactor
> Rename
it to someInstanceMethodToMove()
:
public class GodClass {
public someInstanceMethodToMove() {
new SingleResponsibilityClass().someInstanceMethodToMove();
}
然后右键单击SingleResponsibilityClass
'someInstanceMethodToMove2()
方法并将其Refactor
>Rename
到someInstanceMethodToMove()
: public class GodClass { public someInstanceMethodToMove() { new SingleResponsibilityClass().someInstanceMethodToMove(); }
// ...
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}
Then right click on GodClass
' someInstanceMethodToMove()
method and Refactor
> Inline
:
然后右键单击GodClass
'someInstanceMethodToMove()
方法和Refactor
> Inline
:
public class GodClass {
public static void main(String[] args) {
GodClass c = ...;
new SingleResponsibilityClass().someInstanceMethodToMove();
SingleResponsibilityClass.someStaticMethodToMove();
}
}
public class SingleResponsibilityClass {
private someInstanceMethodToMove() {
// some code 1
}
public static someStaticMethodToMove() {
// some code 2
}
}