java IntelliJ getter/setter 格式(单行与多行)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1785244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:58:21  来源:igfitidea点击:

IntelliJ getter/setter format (single-line versus multi-line)

javaintellij-ideagetter-setter

提问by Marcus Leon

How can you get IntelliJto generate getter/setters accessor methods on one line like this:

如何让IntelliJ在这样的一行上生成 getter/setter 访问器方法:

public String getAbc() { return abc; }

… instead of multiple lines like this:

... 而不是像这样的多行:

public String getAbc() {
   return abc;
}

采纳答案by David Robles

There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA.

没有用于 getter 和 equals/hashcode 的模板。这些是在 IDEA 中硬编码的。

Source

来源

You can see that in this IntelliJ Wishlist

你可以在这个IntelliJ Wishlist 中看到

回答by Alex G

I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour.

我使用的是 IntelliJ IDEA 14.1.0,您可以自定义此行为。

Just use the "Generate..." option, or use Alt+Insertshortcut, and select "Getter and Setter".

只需使用“Generate...”选项,或使用Alt+Insert快捷方式,然后选择“Getter and Setter”。

In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top. Use the "..." button next to the dropdown, to edit the template.

在打开的“选择字段”窗口中,顶部有“获取模板”选项。使用下拉菜单旁边的“...”按钮来编辑模板。

Select "IntelliJ Default" and click the "Copy" button to create a new one named "AlwayStartWithGet", which you can edit.

选择“IntelliJ Default”并单击“Copy”按钮创建一个名为“AlwayStartWithGet”的新文件,您可以对其进行编辑。

Just remove the following section:

只需删除以下部分:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

And replace it with a simple

并用一个简单的替换它

get##

You should be left with:

你应该留下:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

Now you can use the custom template when generating code, by selecting it in the getter template dropdown.

现在您可以在生成代码时使用自定义模板,方法是在 getter 模板下拉列表中选择它。

回答by laffuste

For Idea 2016.

对于 2016 年的想法。

Getter template

吸气剂模板

Merge the last 3 lines into a single line:

将最后 3 行合并为一行:

${name}() { return $field.name; }

Setter template

二传手模板

Add double hash (without a space) at the end of the longest line:

在最长行的末尾添加双哈希(无空格):

[...] ($field.type, $paramName) {##

Merge the last 2 lines into a single line:

将最后两行合并为一行:

$field.name = $paramName; }

Note: as commented by @mindas, you will probably want instead the visual auto folding that doesn't get versioned.

注意:正如@mindas 所评论的那样,您可能需要不进行版本控制的视觉自动折叠。

回答by Noel Ang

You didn't mention what version of IDEA you are using, so I assume the recent 8 or 9.

您没有提到您使用的是哪个版本的 IDEA,所以我假设是最近的 8 或 9。

Check your Code Style settings, under "Alignment and Braces". You should find a "Simple methods in one line" option there.

在“对齐和大括号”下检查您的代码样式设置。您应该在那里找到“一行中的简单方法”选项。

回答by mindas

I don't know why you want to do this, presumably for saving visual space. If so, just use IntelliJ's feature to fold trivial getters/setters and forget about how lines does it take. Folding feature can be found in

我不知道你为什么要这样做,大概是为了节省视觉空间。如果是这样,只需使用 IntelliJ 的功能来折叠琐碎的 getter/setter 并忘记它是如何使用的。折叠功能可以在

Settings -> IDE Settings -> Editor -> Code Folding -> Show code folding outline -> Simple property accessors

设置 -> IDE 设置 -> 编辑器 -> 代码折叠 -> 显示代码折叠大纲 -> 简单属性访问器

回答by Hyman J

Alex G and laffuste provided answers that helped me, but even after you follow these instructions, IntelliJ may still automatically format your code. To stop this from happening, go to Settings -> Editor -> Code Style -> Java (or other language). Click on the tab titled "Wrapping and Braces". Under the title "Keep when reformatting" click the checkbox next to "Simple methods in one line".

Alex G 和 lafuste 提供的答案对我有帮助,但即使您按照这些说明进行操作,IntelliJ 仍可能会自动格式化您的代码。要阻止这种情况发生,请转到设置 -> 编辑器 -> 代码样式 -> Java(或其他语言)。单击标题为“环绕和大括号”的选项卡。在“重新格式化时保留”标题下,单击“一行中的简单方法”旁边的复选框。

For further brevity, you can eliminate the blank lines between the getter and setter methods. To do this, click on the tab titled "Blank Lines". In the section titled "Minimum Blank Lines", find the text box next to "Around method". Enter 0 in the text box.

为了更简洁,您可以消除 getter 和 setter 方法之间的空行。为此,请单击标题为“空白行”的选项卡。在标题为“最小空白行”的部分中,找到“Around 方法”旁边的文本框。在文本框中输入 0。