如何在 IntelliJ 中将局部变量提取为类变量?

edo*_*eld 1 java keyboard-shortcuts intellij-idea

我希望能够将方法中声明的局部变量转换为类变量。请看下面的例子。

// This is my class now
public class FOO {
    public getX() {
        String X = "test"
        return X;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用键盘快捷键,变量 X 应该转换为类变量。最终结果应该是这样的:

// This is what my class should look like afterwards
public class FOO {
    String X = "test"
    public getX() {
        return X;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在 IntelliJ 中做到这一点?

Mic*_*ael 6

突出显示XCTRL+ ALT+F

对于其他类型的提取,将 F 替换为以下字母

  • F = 场
  • P = 参数
  • V = 变量
  • C = 常数(静态场)
  • M = 方法

也可通过

右键单击>重构>提取