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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2117962/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:20:10  来源:igfitidea点击:

How to refactor a static inner class to a top level class in Eclipse?

javaeclipseautomated-refactoring

提问by Grundlefleck

I am having trouble finding the correct refactor option for the following scenario:

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

I have code like this (in Outer.java):

我有这样的代码(在 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 .javasource 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.

我正在寻找一种方法来选择内部,并将其转换为顶级类,在它自己的.java源文件中。我知道这很容易通过复制/粘贴等手动完成,但是内部类在很多地方都被引用,我希望重构选项可以在引用它的任何地方处理更改。

I have tried the option Refactor -> Extract Class...but that does something weird with a field called datathat I don't quite understand.

我已经尝试过这个选项,Refactor -> Extract Class...但它对一个data我不太了解的字段做了一些奇怪的事情。

Eclipse version is 3.5.1 Galileo.

Eclipse 版本是 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.

编辑:不敢相信我忽略了执行此操作的选项。感谢您的所有正确答案,+1 给大家。我仍然需要接受答案,所以如果有更多有用的信息,例如脚本的问题,仍然有留下答案的目的。

回答by Grundlefleck

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

将光标置于内部类型内的任意位置,右键单击并选择:
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 的更高版本中,此重构已重命名为“将类型移动到新文件”

回答by Tom

For completeness, version 4.x of Eclipse has changed terminology and now they call that operation as Move Type to New File...

为完整起见,Eclipse 4.x 版已更改术语,现在他们将该操作称为 Move Type to New File...

Move type to new file

将类型移至新文件

回答by Kelly S. French

Extract Class is not what you want.

提取类不是您想要的。

I just did this using Eclipse 3.4.2

我只是使用 Eclipse 3.4.2 做到了这一点

  1. Select Inner
  2. Right click 'Refactor->Convert Member Type To Top Level...'
  1. 选择内部
  2. 右键单击“重构-> 将成员类型转换为顶级...”

回答by EJB

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.

我有 3.4.1,如果我右键单击内部,> 重构,我有选项“将成员类型转换为顶级..”,这正是您想要的。

回答by Klarth

Use Refactor-> Convert Member to Top Level Type

使用Refactor-> Convert Member to Top Level Type

回答by liuzhengyang

For Intellj2018.1.

对于 Intellj2018.1。

  1. right click in the inner class
  2. Refactor
  3. Move
  4. Move to upper level
  1. 右键单击内部类
  2. 重构
  3. 移动
  4. 移动到上层

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明