类注释
图1 idea默认的类模板
不管能不能懂 VTL 语法,当看到这里#parse("File Header.java")
应该就知道它包含了另一个文件。没错,文件在这:
图2 File Header.java
那我们该怎么修改呢?分情况吧
-
只想修改Class下的模板,那就删除图1中
#parse("File Header.java")
,添加新的注释进去。(推荐)/** * ${DESCRIPTION} * @author * @create ${YEAR}-${MONTH}-${DAY} ${TIME} */
${DESCRIPTION}
是我们自己定义的变量,自定义变量格式有两种方式:$变量名
或${变量名}
,自定义的变量在创建对应的文件时候就必须输入内容 -
想修改所有引用了
File Header.java
的文件模板,修改图2如下。/** * @author * @create ${YEAR}-${MONTH}-${DAY} ${TIME} */
因为变量的命名太明了不过了,所以这里不多讲解,直接贴出官网的英文解释:
- ${PACKAGE_NAME} - the name of the target package where the new class or interface will be created.
- ${PROJECT_NAME} - the name of the current project.
- ${FILE_NAME} - the name of the PHP file that will be created.
- ${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
- ${USER} - the login name of the current user.
- ${DATE} - the current system date.
- ${TIME} - the current system time.
- ${YEAR} - the current year.
- ${MONTH} - the current month.
- ${DAY} - the current day of the month.
- ${HOUR} - the current hour.
- ${MINUTE} - the current minute.
- ${PRODUCT_NAME} - the name of the IDE in which the file will be created.
- ${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
- ${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.
方法注释
idea的方法注释一般通过Live Templates实现
这块内容不了解的可以戳进
-
添加Live Templates
-
输入Abbreviation,你可以键入之获取 Template text中的内容
-
在Template text中键入
/** * @Description: * @author: $user$ * @Param: $params$ * @Return: $return$ * @Date: $date$ $time$ */
其中,
$$
之间的是自定义变量,可以通过Edit variables设置 -
设置applicable contexts
-
勾选 Reformat according to style
-
设置变量的值
点击Edit variables,进入后选择每个变量对应的Expression
-
使用
这种方式需要获取方法的相关信息,所以须在方法内部键入Abbreviation,如果在方法外部使用,将出现获取不到值的情况