Java 中注释代码的最佳方式
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
看一下 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.选择您想要注释的方法,然后同时按 SHIFT、ALT 和 J。
还要花时间了解 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.
按照惯例,这是这样做的方法:
如果您的方法返回任何内容,您将添加一个“@return”,后跟一个解释。
您的 IDE 和标准 JavaDoc 工具将能够解析它。
By convention this is the way to do it:
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.
我个人更喜欢使用 JAutodoc 插件进行评论。看看它。很好。
I personally prefer to use JAutodoc plugin for commenting. Take a look at it. Its good.
这个线程似乎有些混乱。我用来生成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?
最好的方法是使用 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.
我想说,在 java 中注释代码的最佳方法是为您的方法和变量名称提供有意义的名称:)
I'd say that the best way to comment code in java is to provide meaningful names for your methods and variables names :)
最好的方法是使用 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
您需要在将光标置于声明行时同时按 CTRL+ALT+J。
You need to press CTRL+ALT+J in same time having the cursor on the declaration row.
我建议使用 Eclipse 的 shift+alt+j ,并编写函数的描述,以便其他开发人员可以理解该函数可以做什么,并且此自动注释功能将提供 @param 和 @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: