如何在 Eclipse 中将静态内部类重构为顶级类?
我无法为以下场景找到正确的重构选项:
我有这样的代码(在 Outer.java 中):
public class Outer {
// ... class stuff
public static class Inner {
// ... inner class stuff
}
}
我正在寻找一种方法来选择 Inner,并将其转换为顶级类,在它自己的 < code>.java 源文件。我知道这很容易通过复制/粘贴等手动完成,但是内部类在很多地方都被引用,并且我希望重构选项能够在引用的任何地方处理更改。
我尝试过选项 Refactor -> Extract Class...
但这对我不太理解的名为 data
的字段做了一些奇怪的事情。
Eclipse 版本是 3.5.1 Galileo。
如何将静态内部类重构为顶级类?
编辑:不敢相信我忽略了执行此操作的选项。感谢您的所有正确答案,为大家+1。我仍然需要接受答案,所以如果有任何更有用的信息,例如脚本的陷阱,仍然有一个留下答案的目的。
I am having trouble finding the correct refactor option for the following scenario:
I have code like this (in Outer.java):
public class Outer {
// ... class stuff
public static class Inner {
// ... inner class stuff
}
}
I am looking for a way to select Inner, and have it converted to a top level class, in it's own .java
source file. I know this is pretty easy to do manually with copy/paste etc., but the inner class is referenced in a lot of places, and I would like the refactor option to handle the change everywhere it is referenced.
I have tried the option Refactor -> Extract Class...
but that does something weird with a field called data
that I don't quite understand.
Eclipse version is 3.5.1 Galileo.
How do I refactor a static inner class to be a top level class?
Edit: Can't believe I overlooked the option to do this. Thanks for all your correct answers, +1 to you all. I'll still need to accept an answer, so if there is any more useful info, e.g. gotchas with the script, there is still a purpose to leaving an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这太简单了,我不敢相信我错过了它:
将光标放在内部类型中的任意位置,右键单击并选择:
重构->将成员类型转换为顶级...
(默认设置中没有快捷方式)
这会自动提取内部类型,并将其放置在它自己的文件中,与外部类型位于同一包和目录中。
更新
在 Eclipse 的更高版本中,此重构已重命名为“将类型移动到新文件”
This is so easy I can't believe I missed it:
With the cursor anywhere within the inner type, right click and select:
Refactor -> Convert Member Type to Top Level...
(There is no shortcut in the default settings)
This automatically extracts the inner type, and places it in it's own file, in the same package and directory as the outer type.
Update
In later versions of Eclipse this refactoring has been renamed "Move Type to New File"
为了完整起见,Eclipse 4.x 版本更改了术语,现在他们将该操作称为
将类型移动到新文件...
For completeness, version 4.x of Eclipse has changed terminology and now they call that operation as
Move Type to New File...
Extract Class 不是您想要的。
我刚刚使用 Eclipse 3.4.2
Extract Class is not what you want.
I just did this using Eclipse 3.4.2
我有 3.4.1,如果我右键单击“内部”>“重构”,我可以选择“将成员类型隐藏到顶级..”,这正是您想要的。
I have 3.4.1, and if I right-click on Inner, >refactor, I have the option "Covert member Type to top level..", which does exactly what you want.
使用重构 -> 将成员转换为顶级类型
Use Refactor -> Convert Member to Top Level Type
对于Intelj2018.1。
For Intellj2018.1.