Java 中注释代码的最佳方式

发布于 2024-11-18 04:18:41 字数 391 浏览 4 评论 0原文

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

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

// <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)
{

}

已经谢谢了。

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 ?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

风向决定发型 2024-11-25 04:18:41

看一下 Javadoc

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

Take a look at 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.

菩提树下叶撕阳。 2024-11-25 04:18:41

选择您想要注释的方法,然后同时按 SHIFTALTJ

还要花时间了解 JavaDoc,它是一个非常丰富的用于记录代码的领域。

Select the method for which you want comments and press SHIFT, ALT and J together.

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

壹場煙雨 2024-11-25 04:18:41

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

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

}

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

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

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.

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

那伤。 2024-11-25 04:18:41

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

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

对你再特殊 2024-11-25 04:18:41

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

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

江湖彼岸 2024-11-25 04:18:41

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

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

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

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.

荒路情人 2024-11-25 04:18:41

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

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

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

class MyService {
    public void authenticateUser(int userId, String userPassword) {...}
}
心欲静而疯不止 2024-11-25 04:18:41

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

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

您还可以在Window -> 中控制Javadoc注释的生成方式。首选项-> Java->代码风格->代码模板 ->评论

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

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.

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

写给空气的情书 2024-11-25 04:18:41

您需要在将光标置于声明行时同时按 CTRL+ALT+J

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

芯好空 2024-11-25 04:18:41

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

例如:

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

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.

For Example:

/**
 * @param msg
 * will return the converted message from the byte[] msg
 * @return
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文