# 提取字段

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

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

Extract Field  重构允许您声明一个新字段并用所选表达式对其进行初始化。 原始表达式被字段的用法替换。

Procedure: 就地提取字段

1. 将文本光标放置在您想要提取到字段中的代码部分。

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

3. 请选择您要作为字段引入的表达式。

![选择表达式](https://resources.jetbrains.com.cn/help/img/idea/2026.3/extract_field.png)

如果 IntelliJ IDEA 在您的代码中检测到多个出现，它允许您指定要替换的出现。

![提取字段的多次出现](https://resources.jetbrains.com.cn/help/img/idea/2026.3/extract_field_occurrences.png)

您可以按 `Ctrl+Alt+F` (Windows), `⌘ ⌥ F` (macOS), `⌘ ⌥ F` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ F` (macOS System Shortcuts), `Ctrl+Alt+F` (XWin), `Ctrl+Alt+F` (GNOME), `Ctrl+Alt+F` (KDE), `Ctrl+Alt+F` (Emacs), `Ctrl+Alt+F` (Sublime Text), `Ctrl+Alt+F` (Sublime Text (macOS)), `Ctrl+R, F` (Visual Studio), `⌘ R, F` (Visual Studio (macOS)), `Alt+Shift+E` (NetBeans), `Ctrl+Alt+F` (Eclipse), `⌘ ⌥ F` (Eclipse (macOS)) 两次以打开   提取字段   对话框，在那里，您可以指定其他详细信息，如可见性选项或为您的变量初始化的选项。

![提取字段对话框](https://resources.jetbrains.com.cn/help/img/idea/2026.3/extract_field_dialog.png)

## 示例

让我们将  `anotherClass.intValue();`  变量提取到字段中。 因此，IntelliJ IDEA 将所选变量名更改为  `number`  并将其声明为  `private int number`  字段。

|  之前  |  之后  |
| --- | --- |
|     ```JAVA public class Class {     AnotherClass anotherClass;     public void method() {         int a = 1;          int b = a + anotherClass.intValue();         int c = b + anotherClass.intValue();     } }  ```    |     ```JAVA public class Class {     public AnotherClass anotherClass;     private int number;      public void method() {         int a = 1;         number = anotherClass.intValue();         int b = a + number;         int c = b + number;     } }      ```    |

## See also

### 参考

[提取字段对话框](extract-field-dialog.html)

