I. 如何自动添加注释
可通过如下三种方法自动添加注释:
1. 输入“/**”并回车。
2. 用快捷键 Alt+Shift+J。
3. 在右键菜单中选择“Source > Generate Element Comment”。
II. 如何自定义注释格式
通过菜单 Window->Preference 打开参数设置面板,然后选择:
Java -> Code Style -> Code Templates
在右侧选择Comments,将其中的Files项,然后选右边的"Edit",进入编辑模式:
进入编辑模式后就可以自定义注释了。另外可以插入一些变量,如年、日期等等。
最后,确保 Code -> New Java files 中有:"${filecomment}"
III. 导入/导出代码格式模板
通过“导出”和“导入”功能,你可以把自己的模板导出来在其他机器上使用。
操作步骤:打开Window->Preferences->Java->Code Style->Code Templates
如图:
点击“Import”,导入模板codetemplates.xml文件。
附:codetemplates.xml文件内容
<?xml version=
"1.0"
encoding=
"UTF-8"
standalone=
"no"
?><templates><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"
>
/**
* <b>项目名:</b>系统项目名称<br/>
* <b>包名:</b>${package_name}<br/>
* <b>文件名:</b>${file_name}<br/>
* <b>版本信息:</b><br/>
* <b>日期:</b>${date}-${time}<br/>
* <b>Copyright (c)</b> ${year}XX公司-版权所有<br/>
*
*/
</template><template autoinsert=
"true"
context=
"filecomment_context"
deleted=
"false"
description=
"Comment for created JavaScript files"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.filecomment"
name=
"filecomment"
>
/**
*
*/
</template><template autoinsert=
"true"
context=
"delegatecomment_context"
deleted=
"false"
description=
"Comment for delegate functions"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.delegatecomment"
name=
"delegatecomment"
>
/**
* ${tags}
* ${see_to_target}
*/
</template><template autoinsert=
"false"
context=
"typecomment_context"
deleted=
"false"
description=
"Comment for created types"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.typecomment"
name=
"typecomment"
>
/**
* @author kin
*
* ${tags}
*/
</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"
>
/**
*
* <b>类名称:</b>${type_name}<br/>
* <b>类描述:</b><br/>
* <b>创建人:</b>kin<br/>
* <b>修改人:</b>kin<br/>
* <b>修改时间:</b>${date} ${time}<br/>
* <b>修改备注:</b><br/>
* @version 1.0.0<br/>
*
*/
</template><template autoinsert=
"true"
context=
"fieldcomment_context"
deleted=
"false"
description=
"Comment for vars"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.fieldcomment"
name=
"fieldcomment"
>
/**
*
*/
</template><template autoinsert=
"false"
context=
"constructorcomment_context"
deleted=
"false"
description=
"Comment for created constructors"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.constructorcomment"
name=
"constructorcomment"
>
/**
* 创建一个新的实例 ${enclosing_type}.
*
* ${tags}
*/
</template><template autoinsert=
"true"
context=
"overridecomment_context"
deleted=
"false"
description=
"Comment for overriding functions"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.overridecomment"
name=
"overridecomment"
>
/* (non-Jsdoc)
* ${see_to_overridden}
*/
</template><template autoinsert=
"true"
context=
"settercomment_context"
deleted=
"false"
description=
"Comment for setter method"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.settercomment"
name=
"settercomment"
>
/**
* @param ${param} the ${bare_field_name} to set
*/
</template><template autoinsert=
"true"
context=
"settercomment_context"
deleted=
"false"
description=
"Comment for setter function"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.settercomment"
name=
"settercomment"
>
/**
* @param ${param} the ${bare_field_name} to set
*/
</template><template autoinsert=
"false"
context=
"gettercomment_context"
deleted=
"false"
description=
"Comment for getter method"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.gettercomment"
name=
"gettercomment"
>
/**
* ${bare_field_name}
*
* @return the ${bare_field_name}
* @since 1.0.0
*/
</template><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"
>
/**
* ${enclosing_method}(这里用一句话描述这个方法的作用)<br/>
* (这里描述这个方法适用条件 – 可选)<br/>
* ${tags}
*${return_type}
* @exception
* @since 1.0.0
*/
</template><template autoinsert=
"true"
context=
"delegatecomment_context"
deleted=
"false"
description=
"Comment for delegate methods"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.delegatecomment"
name=
"delegatecomment"
>
/**
* ${tags}
* ${see_to_target}
*/
</template><template autoinsert=
"true"
context=
"constructorcomment_context"
deleted=
"false"
description=
"Comment for created constructors"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.constructorcomment"
name=
"constructorcomment"
>
/**
* ${tags}
*/
</template><template autoinsert=
"true"
context=
"overridecomment_context"
deleted=
"false"
description=
"Comment for overriding methods"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.overridecomment"
name=
"overridecomment"
>
/* (non-Javadoc)
* ${see_to_overridden}
*/
</template><template autoinsert=
"false"
context=
"fieldcomment_context"
deleted=
"false"
description=
"Comment for fields"
enabled=
"true"
id=
"org.eclipse.jdt.ui.text.codetemplates.fieldcomment"
name=
"fieldcomment"
>
/**
* ${field}:${todo}(用一句话描述这个变量表示什么)
*
* @since 1.0.0
*/
</template><template autoinsert=
"true"
context=
"gettercomment_context"
deleted=
"false"
description=
"Comment for getter function"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.gettercomment"
name=
"gettercomment"
>
/**
* @return the ${bare_field_name}
*/
</template><template autoinsert=
"true"
context=
"methodcomment_context"
deleted=
"false"
description=
"Comment for non-overriding function"
enabled=
"true"
id=
"org.eclipse.wst.jsdt.ui.text.codetemplates.methodcomment"
name=
"methodcomment"
>
/**
* ${tags}
*/
</template></templates>
|