eclipse 在 Java 中注释代码的最佳方式

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

The best way to comment code in Java

javaeclipsecommentsenterprise

提问by Wassim AZIRAR

What's the best way to comment code in Java, is there a way to generate the function name and the parameters automatically in Eclipse ?

在 Java 中注释代码的最佳方法是什么,有没有办法在 Eclipse 中自动生成函数名称和参数?

For example I'm writting those comments manually :

例如,我正在手动编写这些评论:

// <summary>
// Some comments about the function
// </summary>
// <param name="id">the user ID</param>
// <param name="username">The user password</param>
// <returns></returns>
public Connect(int id, String password)
{

}

Thanks already.

已经谢谢了

回答by wjans

Take a look at Javadoc

看一看Javadoc

Javadocs can easily be generated in Eclipse. You can type /**and it will autocomplete. You can also configure your code templates to automatically generate javadocs.

Javadoc 可以在 Eclipse 中轻松生成。您可以输入/**,它会自动完成。您还可以配置代码模板以自动生成 javadoc。

回答by Brian

Select the method for which you want comments and press SHIFT, ALTand Jtogether.

选择您想要评论的方法并同时按下SHIFTALTJ

Take the time to learn about JavaDoc as well it's a very rich area for documenting your code.

花点时间了解 JavaDoc,它也是记录代码的一个非常丰富的领域。

回答by Mathias Schwarz

By convention this is the way to do it:

按照惯例,这是这样做的方法:

/** Some comments about the function
  * 
  * @param id the user ID
  * @param username The user password
  *
*/
public Connect(int id, String password)
{

}

If your method returns anything, you would add a `@return' followed by an explanation.

如果您的方法返回任何内容,您将添加一个 `@return' 后跟一个解释。

You IDE and the standard JavaDoc tool will be able to parse this.

您的 IDE 和标准 JavaDoc 工具将能够解析它。

回答by Tapas Bose

I personally prefer to use JAutodocplugin for commenting. Take a look at it. Its good.

我个人更喜欢使用JAutodoc插件进行评论。看一看。很好。

回答by bazza2000

There seems to be some confusion on this thread. The key sequence I use to generate javadoc comments is SHIFT+ALT+Jnot CTRL?

这个线程似乎有些混乱。按键顺序我使用生成javadoc注释是SHIFT+ ALT+J不是CTRL

回答by Andrey Adamovich

The best way is to use Javadoc comment format, not the one you shown in the question.

最好的方法是使用 Javadoc 注释格式,而不是您在问题中显示的格式。

In Eclipse, put your cursor on the method name and press Ctrl+Alt+J. It will generate you a Javadoc comment with all parameters listed.

在 Eclipse 中,将光标放在方法名称上并按Ctrl+ Alt+ J。它将为您生成一个 Javadoc 注释,其中列出了所有参数。

You can also control the way Javadoc comment is generated in Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments

也可以在Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments中控制Javadoc注释的生成方式

回答by spot35

The best way is to use JavaDoc and eclipse has built in code templates for doing just that.

最好的方法是使用 JavaDoc,而 eclipse 已经内置了代码模板来实现这一点。

If you want to have the format you've shown here, then you can write your own templates. The templates functionality will allow you to insert variables, of which one will be the method name.

如果您想拥有此处显示的格式,则可以编写自己的模板。模板功能将允许您插入变量,其中之一将是方法名称。

回答by Thiib

You need to press CTRL+ALT+Jin same time having the cursor on the declaration row.

您需要在将光标放在声明行上的同时按CTRL+ ALT+ J

回答by denis.solonenko

I'd say that the best way to comment code in java is to provide meaningful names for your methods and variables names :)

我想说在 Java 中注释代码的最佳方法是为您的方法和变量名称提供有意义的名称:)

class MyService {
    public void authenticateUser(int userId, String userPassword) {...}
}

回答by Darshan Shah

I would suggest to go with the shift+alt+j for Eclipse, and write the description of the function so that other developer can understand what the function can do and also this auto commenting functionality will provide the @param and @return attributes so that you can specify what should be needed and what should be expected in order to execute the function.

我建议在 Eclipse 中使用 shift+alt+j,并编写函数的描述,以便其他开发人员可以了解该函数可以做什么,并且此自动注释功能将提供 @param 和 @return 属性,以便您可以指定为了执行该功能应该需要什么以及应该期望什么。

For Example:

例如:

/**
 * @param msg
 * will return the converted message from the byte[] msg
 * @return
 */