# 提取方法

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

> **TL;DR**
> `重构 | 提取/引入 | 方法`
>
>
>
> `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+R, M` (Visual Studio), `⌘ R, M` (Visual Studio (macOS)), `Alt+Shift+M` (NetBeans), `Alt+Shift+M` (Eclipse), `⌘ ⌥ M` (Eclipse (macOS))

提取方法   重构允许您将可以分组的代码片段移动到一个单独的方法中，并用方法调用替换旧代码。

> **Note:**
> 此重构也可以作为编辑器中的 [intention action](intention-actions.html)使用。 请选择要提取的代码，按下 `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Alt+Enter` (NetBeans), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)) 并选择   提取方法  。

当您提取方法时，您需要检查变量。 如果有一个输出变量，它将作为提取方法的返回值。 如果存在多个输出变量，则可能无法应用  Extract Method 重构，并会出现错误消息。

在这种情况下，有几种解决方法可以让  Extract Method 工作。 例如，您可以引入一个包含所有输出值的特殊 data-class。

> **Note:**
> 提取方法重构具有以下限制：
>
>
>
> * 重构在有对泛型类型的引用时不起作用。
>
> * 重构在自动模式下不支持多个输出值。 在应用重构之前，您必须更改代码。

Procedure: 提取方法

1. 选择您想要提取到方法的代码片段。

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

或者，在选中代码片段时出现的 [浮动工具栏](working-with-source-code.html#floating_toolbar)上，点击   提取  并选择   方法  。

3. 如果 IntelliJ IDEA 检测到代码仅部分重复，它会建议提取一个参数以继续重构。

![部分重复](https://resources.jetbrains.com.cn/help/img/idea/2026.3/partial_duplicates.png)

> **Tip:**
> 要撤销  Extract Method 重构，请按 `Ctrl+Alt+N` (Windows), `⌘ ⌥ N` (macOS), `⌘ ⌥ N` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ N` (macOS System Shortcuts), `Ctrl+Alt+N` (XWin), `Ctrl+Alt+N` (GNOME), `Ctrl+Alt+N` (KDE), `Ctrl+Alt+N` (Emacs), `Ctrl+Alt+N` (Sublime Text), `⌘ ⌥ N` (Sublime Text (macOS)), `Ctrl+R, I` (Visual Studio), `⌘ R, I` (Visual Studio (macOS)), `Ctrl+Alt+N` (NetBeans), `Alt+Shift+I` (Eclipse), `⌘ ⌥ I` (Eclipse (macOS))以调用 [Inline 重构](inline.html)。

> **Tip:**
> 默认情况下，此提取重构将在编辑器中应用。 要更改您的设置以通过 [模态窗口](extract-method-dialog.html)应用重构，请打开   设置  对话框（ `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS))），转到   编辑器 | 代码编辑  ，并在   重构  区域选择   在模态对话框中  。

## 示例

让我们将  `a+b`  表达式提取到一个方法（Kotlin 中的函数）中，并替换重复的内容。

Java:

|  之前  |  之后  |
| --- | --- |
|     ```JAVA public void method() {     int a=1;     int b=2;     int c=a+b;     int d=a+c; }                              ```    |     ```JAVA public void method() {     int a=1;     int b=2;     int c=add(a,b);     int d=add(a,c); } ... private int add(int a, int b) {     return a+b; }                              ```    |

Kotlin:

|  之前  |  之后  |
| --- | --- |
|     ```KOTLIN fun method(){     val a = 1     val b = 2     val c = a + b     val d = a + b }                              ```    |     ```KOTLIN fun method(){     val a = 1     val b = 2     val c = add(a, b)     val d = add(a, b) } private fun add(a: Int, b: Int) = a + b                              ```    |

## 使用 Java records 提取方法

从 Java 16 版本开始，您可以使用 Java records 提取方法。 在有多个变量时，这可能会有所帮助。 在这些情况下，IDE 首先建议将这些变量包装到一个新的 record 或 bean 类中，然后执行方法提取。

![使用 Java records 提取方法](https://resources.jetbrains.com.cn/help/img/idea/2026.3/java_records_refactoring.animated.gif)

之前:

```JAVA
class SomeClass {

    public static void main(String[] args) {
        int values[] = {1, 2, 3, 4, 5};
        int sum = 0;
        int num = 0;

        for (int n : values) {
            sum += n;
            num++;
        }

        System.out.println("Sum: " + sum + " - Num: " + num);
    }
}

```

之后:

```JAVA
class SomeClass {

    public static void main(String[] args) {
        Statistics result = getStatistics();

        System.out.println("Sum: " + result.sum() + " - Num: " + result.num());
    }

    private static Statistics getStatistics() {
        int values[] = {1, 2, 3, 4, 5};
        int sum = 0;
        int num = 0;

        for (int n : values) {
            sum += n;
            num++;
        }
        Statistics result = new Statistics(sum, num);
        return result;
    }

    private record Statistics(int sum, int num) {
    }
}

```

