# 移动和复制重构

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/idea/llms.txt).

复制   重构允许您在不同的包中创建类的副本。 它还允许您在不同的目录或包中创建文件、目录或包的副本。

> **Tip:**
> 您可以通过上下文菜单访问所有重构操作。

Procedure: 执行复制重构

1. 选择一个您想要重构的标识符（例如，编辑器中的类或   项目   工具窗口中的文件）。

2. 在主菜单中，转到  `重构 | 复制`  或按 `F5` (Windows), `F5` (macOS), `F5` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ Y` (macOS System Shortcuts), `F5` (XWin), `F5` (GNOME), `F5` (KDE), `F5` (Emacs), `F5` (Sublime Text), `F5` (Sublime Text (macOS)), `F5` (Visual Studio), `F5` (Visual Studio (macOS)), `F5` (NetBeans), `F5` (Eclipse), `F5` (Eclipse (macOS))。

3. 在   复制  对话框中，指定副本的名称和位置，然后点击   OK  。

移动  重构允许您在项目的源根之间移动包和类，将类成员移动到其他类，并将内部类提升到上层层级。 例如，如果一个方法或字段在另一个类中使用得比在其自身类中更多，您可以对其执行移动重构。

Procedure: 执行移动重构

1. 选择您想要重构的标识符。

2. 在主菜单中，转到  `重构 | 移动`  或按 `F6` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Ctrl+M` (NetBeans), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS))。

3. 在打开的对话框中，根据您选择的重构项，指定相应的选项，然后点击   重构   (   OK   对于一个 package)。 您也可以点击   预览  ，如果可用，以预览潜在更改。 有关对话框的更多信息，请参阅 [移动对话框](move-dialogs.html)部分的相关文章。

4. 指定目标目录、文件名和包名的路径。

> **Note:**
> 当您对包执行移动重构时，您需要指定将包及其内容移动到何处（另一个包、源根或另一个目录）。

> **Tip:**
> 要撤销重构，请按 `Ctrl+Z` (Windows), `⌘ Z` (macOS), `⌘ Z` (IntelliJ IDEA Classic (macOS)), `⌘ Z` (macOS System Shortcuts), `Ctrl+Z` (XWin), `Ctrl+Z` (GNOME), `Ctrl+Z` (KDE), `Ctrl+Shift+Minus` (Emacs), `Ctrl+Z` (Sublime Text), `⌘ Z` (Sublime Text (macOS)), `Ctrl+Z` (Visual Studio), `⌘ Z` (Visual Studio (macOS)), `Ctrl+Z` (NetBeans), `Ctrl+Z` (Eclipse), `⌘ Z` (Eclipse (macOS))。

> **Tip:**
> 移动 重构也可从 [UML 类图](class-diagram.html)中使用。

Procedure: 将静态方法移动到另一个类

1. 在编辑器中打开您的类，将文本光标放置在您要移动的静态方法上，然后按 `F6` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Ctrl+M` (NetBeans), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS)) (  `重构 | 移动` )。

移动静态成员   对话框将打开。

2. 在   到(完全限定名称)   字段中，输入要移动方法列表中所选成员的类的全限定名称。

您可以点击 ![省略号](https://resources.jetbrains.com/help/img/idea/2026.3/app.general.ellipsis.svg) 图标来选择或搜索现有类。

3. 在   需要移动的成员(仅限 static)  字段中，选中您要移至另一个类的方法旁边的复选框。

列表显示了当前类中检测到的所有静态方法。

4. 点击   重构   以继续，或点击   预览   以在实际重构之前检查结果。

![移动成员对话框](https://resources.jetbrains.com/help/img/idea/2026.3/move_static_members.png)

Procedure: 将实例方法移动到另一个类

如果此方法在您的项目中有一个类型参数，您可以将实例（非静态）方法移动到不同的类中。 在其他任何情况下，您需要首先使此方法  `static` 。

![有关在没有类型参数的情况下移动实例方法的消息](https://resources.jetbrains.com/help/img/idea/2026.3/move_instance_method_message.png)

1. 在编辑器中，将文本光标放在实例方法上，然后按 `F6` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Ctrl+M` (NetBeans), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS)) (  `重构 | 移动` )。

移动实例方法   对话框将打开。

2. 从   选择实例表达式  列表中选择目标类以移动实例方法。

潜在移动目标列表包括当前类中的方法参数的类和字段的类。

3. 在   可见性  区域中，选择目标方法的首选可见性修饰符。

4. 在   为 'parameter reference' 形参选择一个名称  字段中，输入所需的参数名。

执行重构时，参数将被添加到正在移动的方法中，所有对当前类的参数引用将被替换。

5. 点击   重构   以继续，或点击   预览   以在实际重构之前检查结果。

![移动实例方法对话框](https://resources.jetbrains.com/help/img/idea/2026.3/Move_instance_method_dialog.png)

## 实例方法示例

让我们将  `获取名称`  实例方法从  `测试`  类移动到  `Car`  类。

Before:

```JAVA
import java.lang.reflect.InvocationTargetException;

public class Test {
    public static void main(String[] args) throws Exception {
        Car c= new Car();
        System.out.println(new Test().getName(c));
    }

    String getName(Car car){
        System.out.print(toString());
        return car.name;
    }
}

class Car {
    String name = "Default Car";

    Car() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
    }
}

```

After:

```JAVA
import java.lang.reflect.InvocationTargetException;

public class Test {
    public static void main(String[] args) throws Exception {
        Car c= new Car();
        System.out.println(c.getName(new Test()));
    }
}

class Car {
    String name = "Default Car";
    Car() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
    }

    String getName(Test anotherObject){
        System.out.print(anotherObject.toString());
        return this.name;
    }
}

```

## See also

### 开始

[选择代码结构](working-with-source-code.html#editor_code_selection)

### 参考

[移动对话框](move-dialogs.html) [移动命名空间对话框](move-namespace-dialog.html)

### 外部链接

[https://blog.jetbrains.com/webide/2013/03/refactoring-for-classes-in-phpstorm-6-move-class/](https://blog.jetbrains.com/webide/2013/03/refactoring-for-classes-in-phpstorm-6-move-class/)

