简体   繁体   English

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

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

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 .java source file. 我正在寻找一种方法来选择Inner,并将其转换为顶级类,在它自己的.java源文件中。 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. 我知道这很容易用复制/粘贴等手动完成,但内部类在很多地方被引用,我希望refactor选项能够处理引用它的每个地方的变化。

I have tried the option Refactor -> Extract Class... but that does something weird with a field called data that 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. 感谢您的所有正确答案,给大家+1。 I'll still need to accept an answer, so if there is any more useful info, eg gotchas with the script, there is still a purpose to leaving an answer. 我仍然需要接受一个答案,所以如果有更多有用的信息,例如与脚本有关,那么仍然有一个留下答案的目的。

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的更高版本中,此重构已重命名为“将类型移动到新文件”

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

将类型移动到新文件

Extract Class is not what you want. Extract Class不是你想要的。

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...' 右键单击“重构 - >将成员类型转换为顶级...”

For Intellj2018.1. 对于Intellj2018.1。

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

在此输入图像描述

在此输入图像描述

我有3.4.1,如果我右键单击Inner,> refactor,我有选项“Covert member Type to top level ..”,这正是你想要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM