Preface: Have you used eclipse shortcut ALT + Shift + J? Have you read the source? If you've seen it, have you noticed the comments on the source code? Do you know why see the source code comments Some of the parameters can be directly clicked jump?
First, define a simple person class, three attributes, a name, an age, a gender, a constructor with all the attribute parameters, what would you write?
public class person { private String mname; private int mAge; private int msex; public person (final String name, final int age, final int sex) {super (); this . Mname = name; this . MAge = age; this . msex = sex; }}
I believe that no one is doing the project is so dry to write it! No more notes! Here is a simple example, from the attribute name can see the meaning, if it is difficult to understand a bit, the amount of code increases, it seems to be a headache.
1. How to quickly generate documentation comments
In fact, Eclipse has a quick way to generate documentation comments, the cursor is positioned to the class, property or function to be annotated, and then right-Generate Element, Source--Comment, I prefer to use the shortcut ALT + Shift + J to automatically generate Commented out!
Incidentally, a little bit of basic skill, the bottom right of the figure
- Generate Constructor using
Fields ...
It is possible to quickly generate constructors with attribute parameters; (I said to create a multi-parameter constructor can be generated so quickly)
- Generate Getters and Setters ...
Fast generation of properties for the picker and the setup; (This function school teacher to let us hit a little bit of code did not say, in the internship only to know this function)
- Override/implement Methods ...
The ability to quickly select the functions of the superclass to be rewritten or implemented;
And then the code that automatically generates the comments turns out this way.
/** * @ClassName person * @Description human * @author AZZ * @Date August 6, 2015 PM 3:27:3 9 * @version 1.0.0 * * Public class person { /** * @Field @age : Age */ Private intMAge;/** * @Field @name : Name * * PrivateString Mname;/** * @Field @sex : Gender * * Private intMsex;/** * @Description constructor * @param Age * @param name * @param Sex gender * / Public Person(intAge, String name,intSex) {Super(); This. MAge = age; This. mname = name; This. msex = sex; }}
Although the code is getting longer, the annotations are clear, easy to read, and, crucially, the document annotations allow you to be prompted in other places where the class, the method, and the property are used.
Does your code look the same when you add a comment? I think it should be different. Because I modified the comment template! ~ so you see there will be some custom tags such as "@ClassName", "@Description", "Field". If you like this template can go to see the 3rd how to change.
2. Meaning of fields in document comments (emphasis)
In the document comments with some fields to indicate the information, can be very clear to tell others the function/class, and the document comment is very good is the other place when you put the mouse on the function/class, you can see the comments you wrote earlier.
In the document comment code snippet, the default field with
- (empty) The text written on all labels becomes the key text that describes the function
- @author Author Information
- @param parameter information
- @return Return information
- @exception exception Information
- @throws Throw exception information (@exception and @throws have the same effect as tested)
- @category Classification Information
- @since from which version to start
Test Code Snippet
/** * Test Method-Test the display of individual comment tags * @author Author Information-AZZ * @param param input parameters * @return Back parameter * @throws Exception parameter Illegal exception * @exception illegalargumentexception param less than 0 or param greater than 100 * @category Classification Information * @since JDK1.0 */ Public Boolean Test(intParamthrowsException {if(Param <0|| param > -) {Throw NewException ("wrong param"); }return false; }
Placing the mouse over the test will show the following
- @see Some functions need to be identified with other classes or functions or properties.
- @see #本类函数名/property name to view other functions or properties
- @see package name. Class name to view other classes
Click to jump to display the corresponding class/function/attribute Comment
- @deprecated indicates that the function is not recommended, and it is written in this tab why it is not recommended and provides a new way to replace the method. After adding this tag, the comment will not be prompted, but the function name will be drawn as a strikethrough
- @ Custom tag name such as @date @Description, etc., you can customize some of the label name, these labels will automatically arrange the comments below the default label
In addition, in the documentation comments, such as @param's interpretation, sometimes we need to refer to other parameters or classes or functions. For example, now define two integer constants in the person class, identifying men and women, and in the setSex() function, I would like to prompt the user to set the two constants I have given, so I can do this: use {@link #函数名/property name} to link this class of properties/functions with {@link the package name. Class name} To link other classes (Did you think of @see?)
/** * @Field @MALE : Men * * Public Static intMALE =0;/** * @Field @FEMALE : Female * * Public Static intFEMALE =1;/** * The msex to set * @param sex either {@link #FEMALE} or {@link #MALE} * Test the link method {@link #tes T (int)} * Test Link class {@link Com.test.note.Person} */ Public void Setsex(intSex) { This. msex = sex; }
Put the mouse on the function name
Click to jump comments
3. How to modify a comment template
Not around the circle, directly give our company in the template.
To find out more about searching for the keyword Eclipse comment template, you can customize the template yourself.
How to use: Open Java-and Code Style, Preferences, Window, Eclipse, Windows
1. Click on the code Templates, Import ... "Mycodetemplates.xml"
2. Click Formatter, Import ... "Myformatter.xml"
If you have any questions, please leave a message to tell me!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android Comment Tips" above comments on the Android function how did you write it? (in Eclipse)