如何在 Eclipse 中将静态内部类重构为顶级类?

发布于 2024-08-18 19:05:14 字数 593 浏览 3 评论 0原文

我无法为以下场景找到正确的重构选项:

我有这样的代码(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吲‖鸣 2024-08-25 19:05:14

这太简单了,我不敢相信我错过了它:

将光标放在内部类型中的任意位置,右键单击并选择:
重构->将成员类型转换为顶级...

(默认设置中没有快捷方式)

这会自动提取内部类型,并将其放置在它自己的文件中,与外部类型位于同一包和目录中。

更新

在 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"

∞觅青森が 2024-08-25 19:05:14

为了完整起见,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...

Move type to new file

预谋 2024-08-25 19:05:14

Extract Class 不是您想要的。

我刚刚使用 Eclipse 3.4.2

  1. 选择内部
  2. 右键单击“Refactor->Convert Member Type To Top Level...”

Extract Class is not what you want.

I just did this using Eclipse 3.4.2

  1. Select Inner
  2. Right click 'Refactor->Convert Member Type To Top Level...'
青柠芒果 2024-08-25 19:05:14

我有 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.

初见 2024-08-25 19:05:14

使用重构 -> 将成员转换为顶级类型

Use Refactor -> Convert Member to Top Level Type

傲鸠 2024-08-25 19:05:14

对于Intelj2018.1。

  1. 在内部类中右键单击
  2. Refactor
  3. Move
  4. 移至上层

在此处输入图像描述

在此处输入图像描述

For Intellj2018.1.

  1. right click in the inner class
  2. Refactor
  3. Move
  4. Move to upper level

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文