Eclipse,将java方法重构为另一个类 [英] Eclipse, refactoring a java method into a another class

查看:319
本文介绍了Eclipse,将java方法重构为另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将classA中的Java方法重构(移动)到classB中,并更新对方法的所有引用?

How can I refactor(move) a Java method in classA into classB and have all references to the method to it updated?

在Eclipse中是否支持?

Is this supported in Eclipse ?

推荐答案

,您可以右键单击并选择移动。

For a static method, you can right click and select 'Move'.

Obj1.myMethod()

然后将移动到

Obj2.myMethod()

,eclipse将修复您的导入等。

and eclipse would fix your imports etc.

对于非静态方法,根据classA和classB之间的关系,这可能无法正常工作。

For a non-static method, this may not work depending on the relationship between classA and classB.

Obj1 myobj1 = new Obj1();
myobj1.myMethod();
myobj1.myOtherMethod();

如果将myMethod()移动到其他类,则重构将不得不更改对象初始化。如果myOtherMethod没有被移动,则不能将myobj1的类型更改为Obj2,因为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.

这篇关于Eclipse,将java方法重构为另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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