开发需读

Java项目代码注释规范

1. 导入代码注释模板

  • 下载代码注释模板文件codetemplates.xml
  • 打开本机的Eclipse,按照这样的步骤Window->Preference->Java->Code Style->Code Template打开代码模板设置面板;
  • 选择import,导入codetemplates.xml
  • 勾选中Automatically add comments for new methods and types,选择OK就完成导入了。

2. 应用注释模板

  • 完成以上操作后,我们新建一个类文件时,默认会自动生成该类的说明;
  • 如果要对一个类(属性,方法)手动生成注释,在定义完成后,选中该类(属性,方法),使用ALT+SHIFT+J快捷键,即可按模板生成注释;
  • 模板中有TODO提示内容的,是需要自己手动去修改的,比如类的描述,方法的描述等

下面是利用模板生成注释的例子:

/**
 * @description: TODO(描述这个类的作用)
 * @author HP
 * @date 2014-08-22
 */

public class CommentTest {

    /**
    * @fields id : TODO(描述这个变量的作用)
    */
    private int id;
    /**
    * @description: TODO(描述这个方法的作用)
    * @author: HP
    * @param param1
    * @param param2
    * @return
    */
    public int doOperate(int param1,int param2){
        return 0;
    }

    /**
    * @description: 这个方法用来处理一个简单的流程
    * @author: HP
    * @param param1
    * @param param2
    * @return
    */
    public int doOperate2(int param1,int param2){
        return -1;
    }

3. 模板codetemplates.xml内容

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
    <template autoinsert="false" context="methodcomment_context"
        deleted="false" description="Comment for non-overriding methods"
        enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.methodcomment"
        name="methodcomment">
        /**
         * @description: ${todo}(描述这个方法的作用)
         * @author: ${user}
         * ${tags}
         */
    </template>
    <template autoinsert="false" context="filecomment_context"
        deleted="false" description="Comment for created Java files" enabled="true"
        id="org.eclipse.jdt.ui.text.codetemplates.filecomment" name="filecomment" />
    <template autoinsert="false" context="fieldcomment_context"
        deleted="false" description="Comment for fields" enabled="true"
        id="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name="fieldcomment">
        /**
         * @fields ${field} : ${todo}(描述这个变量的作用)
         */
    </template>
    <template autoinsert="false" context="typecomment_context"
        deleted="false" description="Comment for created types" enabled="true"
        id="org.eclipse.jdt.ui.text.codetemplates.typecomment" name="typecomment">
        /**
         * @description: ${todo}(描述这个类的作用)
         * @author ${user}
         * @date ${date}
         */
    </template>
</templates>