How to write a Java template

Source: Internet
Author: User

What is the most annoying thing for programmers? Look under:

Repeat, repeat , repeat. important thing to say three times!! For example, when creating a thread's inner class:

Thread t = new Thread (new Runnable () {public    void run () {         //TODO    }});

There's always a way to be a programmer, haha. Java has a template this thing, very convenient.

Every time we hand lazy, do not always have to hand the cheap press alt+/ key, to get code hints? Ha ha.

Well, there's no template for creating anonymous thread classes in the Eclipse Java template! (At least I didn't find it).

The programmer's famous assertion is: create it without it !

First, we need to know how template templates are written. The first step: see how to write in Eclipse.

For the For Loop example, open the eclipse->reference->template and think of the following:

To parse the template statement:

${index} does not mean that we often knock on the I, J, K//${array} Of course the name of the array to be indexed, it will automatically search for the name of the array you want to traverse oh!for (int ${index} = 0; ${index} < ${array}.lengt H ${index}++) {    //${cursor} is the cursor Location code    ${line_selection}${cursor}}

In fact, the template is similar to the shoe code! Just have a set of rules to represent variables, and this set of rules is in Eclipse help

Inside. Road (look):

There are too many things in it, we pick a few common, understand the wording, almost understand! Recommended for | foreach | Try

Homemade Template

Said so much, now to come to the actual combat below! To create a thread anonymous inner class as an example, long ago to see you are not pleasing to the eye, haha!

First create a new template in Eclipse (press the new key), enter the template name, the TD is good, pay attention to not and other repetition Oh!

Thread ${thread:newname (thread)} = new Thread (new Runnable () {    @Override public    void Run () {        //todo       ${ Line_selection}${cursor}}    );

Look at the effect: enter TD and Press alt+/ :

O! Isn't it simple! No more worrying about repetition!

Here is a common example of the pattern template, the end of it! ^_^

Private ${type} () {}private final static ${type} instance = new ${type} ();p ublic static ${type} getinstance () {return Instan CE;}

Look at the effect:

How to write a Java template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.