Java @Override 注释后没有换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23193177/
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-08-13 21:15:59  来源:igfitidea点击:

No line break after @Override annotation?

javaideandroid-studio

提问by Running Turtle

When overriding methods, android studio automatically inserts a line break after the @Overrideannotation.

覆盖方法时,android studio 会自动在@Override注释后插入换行符。

How can I have the IDEinsert the @Overridewithout the line break and have it immediately followed by the method declaration (ie on the same line ?):

我怎样才能IDE@Override没有换行符的情况下插入并立即跟随方法声明(即在同一行?):

@Override public Fragment getItem(int position) { ... }

instead of:

代替:

@Override
public Fragment getItem(int position) { ... }

采纳答案by singularhum

File -> Settings -> Code Style -> Wrapping and Braces -> Method annotations and set to Do not wrap.

File -> Settings -> Code Style -> Wrapping and Braces -> Method annotations 并设置为Do not wrap

Note: Changing the setting will not actually reformat all existing ones. It just won't be wrapped when inserted automatically by the IDE, as you want in your question. This applies to all annotations for methods.

注意:更改设置实际上不会重新格式化所有现有设置。正如您在问题中所希望的那样,当 IDE 自动插入时,它不会被包装。这适用于方法的所有注释。

enter image description here

在此处输入图片说明

回答by Denis Kulagin

It's perfectly valid from the viewpoint of Java syntax to have @Override annotation on the same line as method signature. One thing to do is to tune your IDE autoformatiing settings.

从 Java 语法的角度来看,@Override 注释与方法签名在同一行是完全有效的。要做的一件事是调整您的 IDE 自动格式化设置。

回答by Vegard

It is possible to have it on the same line, but I would advise against it. It is a common convention to always have the annotation on a separate line.

可以将它放在同一条线上,但我建议不要这样做。始终将注释放在单独的行上是一种常见的约定。

  • not following conventions without any reason to makes it inconvenient for developers working on several projects or changing project
  • when you use many annotations the lines will very easily break ~120 chars, making the code less readable
  • 没有任何理由不遵守约定,这会给开发多个项目或更改项目的开发人员带来不便
  • 当您使用许多注释时,这些行很容易中断约 120 个字符,从而降低代码的可读性

if you still want them on the same line, this is a solution: Intellij code formatting, Java annotations on new lines

如果您仍然希望它们在同一行,这是一个解决方案: Intellij 代码格式,新行上的 Java 注释