IDEA注释模板设置

注:此文章一部分由转载而来,本人对其进行了一些补充。

类注释模板和方法注释模板

类注释模板:

方法注释模板:

一、首先我们来设置IDEA中类的模板:(IDEA中在创建类时会自动给添加注释)

  1. File–>settings–>Editor–>File and Code Templates–>Files

    我们选择Class文件(当然你要设置接口的还也可以选择Interface文件)

    (1)${NAME}:设置类名,与下面的${NAME}一样才能获取到创建的类名

    (2)TODO:代办事项的标记,一般生成类或方法都需要添加描述

    (3)${USER}、${DATE}、${TIME}:设置创建类的用户、创建的日期和时间,这些事IDEA内置的方法,还有一些其他的方法在绿色框标注的位置,比如你想添加项目名则可以使用${PROJECT_NAME}

    (4)1.0:设置版本号,一般新创建的类都是1.0版本

  1. 效果图展示

二、设置方法注释模板

IDEA还没有智能到自动为我们创建方法注释,这就是要我们手动为方法添加注释,使用Eclipse时我们生成注释的习惯是

/**+Enter,这里我们也按照这种习惯来设置IDEA的方法注释

  1. File–>Settings–>Editor–>Live Templates

    (1)新建组:命名为userDefine

    (2)新建模板:命名为*

    因为IDEA生成注释的默认方式是:/*+模板名+快捷键(比如若设置模板名为add快捷键用Tab,则生成方式为/*add+Tab),如果不采用这样的生成方式IDEA中没有内容的方法将不可用,例如获取方法参数的methodParameters()

    获取方法返回值的methodReturnType()

    (3)设置生成注释的快捷键

    (4)设置模板:模板内容如下

    注意第一行,只有一个*而不是/*

    在设置参数名时必须用${参数名}$的方式,否则第五步中读取不到你设置的参数名

*
 * @Author chengpunan
 * @Description //TODO $end$
 * @Date $time$ $date$
 * @Param $param$
 * @return $return$
 **/
  • 如果使用/生成的模板注释将会是如下效果:所以我们要去掉最前面的/*

    (5)设置模板的应用场景

    点击模板页面最下方的警告,来设置将模板应用于那些场景,一般选择EveryWhere-->Java即可

    (如果曾经修改过,则显示为change而不是define)

    (6)设置参数的获取方式

    选择右侧的Edit variables按钮

    PS:第五步和第六步顺序不可颠倒,否则第六步将获取不到方法

    选择每个参数对应的获取方法(在下拉选择框中选择即可)

    (7)效果图
    创建方法,在方法上面写:/*+模板名+Enter–>/**+Enter=

创建Java类时,自动在文件头中添加注释

  1. File–>settings–>Editor–>File and Code Templates–>includes–>FileHeader

在这里插入图片描述
参数在下面的Description中有介绍:

This is a built-in template. It contains a code fragment that can be included into file templates (Templates tab) with the help of the #parse directive. The template is editable. Along with static text, code and comments, you can also use predefined variables that will then be expanded like macros into the corresponding values. 
Predefined variables will take the following values:
${PACKAGE_NAME}
 
name of the package in which the new file is created
${USER}
 
current user system login name
${DATE}
 
current system date
${TIME}
 
current system time
${YEAR}
 
current year
${MONTH}
 
current month
${MONTH_NAME_SHORT}
 
first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL}
 
full name of the current month. Example: January, February, etc.
${DAY}
 
current day of the month
${DAY_NAME_SHORT}
 
first 3 letters of the current day name. Example: Mon, Tue, etc.
${DAY_NAME_FULL}
 
full name of the current day. Example: Monday, Tuesday, etc.
${HOUR}
 
current hour
${MINUTE}
 
current minute
${PROJECT_NAME}
 
the name of the current project
在使用Idea进行编码时,你可以设置注释模板来自动生成注释。首先,启用"Enable Live Templates"选项,这样在创建新文件时才会自动生成你设置的模板注释。 然后,可以设置方法注释模板。你可以简单设置或者稍微花一些时间进行定制。如果没有找到满意的模板,你可以研究一下自定义模板,供大家参考。 在设置模板时,你需要填写注释模板的内容,可以使用${参数名}$的格式来表示参数名。 通过以上步骤,你就可以设置Idea注释模板了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [idea注释模板设置(史上最全!!!)](https://blog.csdn.net/AwesomeJokerWang/article/details/117784282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [IDEA设置注释模板的方法](https://blog.csdn.net/dianluo4096/article/details/102039848)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值