In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the Eclipse code template function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The Code template feature in Eclipse. What is a code template? For example, a very common application, how to create a main function in a class?
Isn't that easy? Before I lost my voice, I saw you tapping quickly on the keyboard, and it was done.
OK, type quickly. However, should you think about the need for a letter-by-letter knock for such a very fixed code?
As an IDE, one of the design philosophies is to help users do what they can. In this case, it's time for Eclipse code templates to work.
We just need to type main in the class body, and then use the code of Eclipse to prompt the shortcut key (the default is Alt+/). When we enter, we can see that Eclipse automatically completes the complete definition of the main function for us, isn't it very convenient?
This is just a simple example, but Eclipse also provides a lot of code templates by default. Open Windows- > Preferences- > Java- > Editor- > Templates, and you can see a list of all the defined code templates.
To take a more common example, you often need to output some data in the console in the code.
System.out.pringln ("hello, world")
Before, if you are used to typing letter by letter, now you can try typing sysout, and then press Alt+/, the whole sentence is automatically completed, and the cursor is very considerate in parentheses, convenient for you to continue typing, isn't it powerful?
I sometimes encounter another kind of problem in programming, such as I want to add some code to the try/catch block for exception detection. With the help of the code template, you don't have to type this part of the code by hand or worry about the code indentation, you just need to select the code block to be processed, then press Alt+Shift+Z, and when you enter, Eclipse will automatically pack the try/catch statement for the selected code block.
Although Eclipse has provided us with a lot of useful templates by default, it is certainly not enough, so we need to try to create our own code templates.
Let's take a very realistic example to illustrate that in actual development, we often need to test the execution time of a piece of code. Generally speaking, we will use the following methods.
Record the timestamps before and after code execution respectively, and then calculate the difference between the two timestamps. This code is very rigid, and it's crashing if you type it manually every time, but it's much more convenient if you use the Code template feature to do it.
Open Windows- > Preferences- > Java- > Editor- > Templates, click New..., to create a new code template, enter the code as shown in the following figure, and create a template called "xinneng".
(note: all "${}" are template variables, such as ${line_selection} represents the code snippet selected by the current cursor, ${cursor} indicates the location of the cursor after code generation, and there are many parameters you can refer to the help documentation provided by Eclipse.)
When finished, select the code block to be tested, use the shortcut key Alt+Shift+Z, you can see that there is a xinneng in the menu, click xinneng (), and Eclipse automatically adds the test code we just entered above and below the selected code block, isn't it cool?
Thank you for reading this article carefully. I hope the article "how to use the Code template function of Eclipse" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.