Table of contents
Two: Annotate the author's name and date and other information to the Java method
1. Unmodifiable template: Postfix Completion
2. Modifiable templates: Live Templates
One: Automatically annotate information such as the author's name and date at the beginning of the Java class
In this way, as long as we create a class in the future, information such as the author's name and date and time will be automatically displayed for easy recording!
第一步:File--->Settings--->Editor--->Code Style--->File and Code Templates
Step 2: Select File Header to set the comment information
Common preset variables are:
Let's write a simple annotation message:
/**
*@Author:朗朗乾坤
*@Package:${PACKAGE_NAME}
*@Project:${PROJECT_NAME}
*@name:${NAME}
*@Date:${DATE} ${TIME}
*@Filename:${NAME}
*/
Copy the comment information directly into it
Step 3: In this way, every time we create a class in the future, annotation information will be automatically generated, and the effect will be displayed:
Two: Annotate the author's name and date and other information to the Java method
In IDEA , Postfix Completion has many built-in coding templates , users only need to type a simple prefix to generate code; but these templates are fixed and cannot be changed . Since the Postfix Completion template cannot be changed, IDEA provides a Live Templates template that users can customize .
1. Unmodifiable template: Postfix Completion
File--->Settings--->Editor--->General--->Postfix Completion, there are many built-in unmodifiable templates
For example: some commonly used templates; given a parent class Animals, a subclass Bird, and the subclass has a unique doOther() method, create objects animals and bird
(1) animals.arg is equivalent to adding a bracket (animals)
(2) animals.cast is equivalent to forced type conversion ((Bird) animals)
(3) animals.castvar is equivalent to mandatory type conversion and assignment Bird animals1 = (Bird) animals;
(4) inst is equivalent to instance and then transformed if (bird instanceof Object) { Object o = (Object) bird; }
(5) animals.inst is equivalent to animals instanceof Bird ? ((Bird) animals) : null;
(6) iter is equivalent to enhancing the for loop for (String arg : args) { }
itar is equivalent to a normal for loop
(7) animals.return is equivalent to return animals;
(8) souf is equivalent to System.out.printf("");
sout is equivalent to System.out.println();
soutv is equivalent to System.out.println("bird = " + bird);
(9) animals.null and ifn are equivalent to if (animals == null) { }
(10) animals.notnull and animals.nn are equivalent to if (animals != null) { }
(11) list.for generates a list for loop for (Object o : list) { }
(12) psfs is equivalent to public static final String
(13) prsf is equivalent to private static final
2. Modifiable templates: Live Templates
File--->Settings--->Editor--->Code Style--->Live Templates, there are many built-in modifiable templates; we can modify existing templates, or create a template of our own
For example: Create a comment template and define a shortcut key, so that as long as we press this shortcut key in the future, we will complete the comment on the method:
Step 1: Create a template group: myTemplates
Step 2: Create a template: mdes
In a custom template, you can declare variables in the form of $name$ , and then use the built-in function to dynamically assign values to the variables.
/**
*@Date:$date$ // date变量下面会用内置函数进行赋值
*@Author:
*@return:
*
*/
Step 3: Click Edit variables and use the built-in function date() to dynamically assign values to date variables
Step 4: Click Define at the bottom to set the effective scope of the template. If you are not sure, just select Everywhere
Step 5: In this way, comment on the method in the future, and there will be a prompt if you type m first:
Then press Enter to automatically generate comment information: