idea 模板編程知識小結(jié)
模板編程是idea的強大功能,也提高了開發(fā)人員的編程效率,比如輸入main函數(shù):
public static void main(String[] args){}
正常情況下我們需要每個字母挨個輸入,但是這樣輸入太慢了,有了模板編程,我們只需要輸入psvm或者main,然后回車,就會輸出
public static void main(String[] args){}
,是不是大大的提高了編碼速度。這里對模板編程進行簡單的介紹。
一、模板編程簡介
模板編程的位置如下圖:File-->settings-->Editor
其中,Editor-->General-->Postfix Completion 和 Editor-->Live Templates下面都有模板編程的配置,不同的是Live Templates下的模板是可以新建和修改的
java編程常用的模板我在上圖中標(biāo)注出來了
二、常用模板
先介紹一下常用的、idea自帶的模板
1. static final 變量
prsf: private static final
psf: public static final
psfi: public static final int
psfs: public static final String
2. main函數(shù)
psvm/main: public static void main(String[] args) { }
3. for循環(huán)
fori: for (int i = 0; i < ; i++) { }iter: for (String arg : args) { }itar: for (int i = 0; i < args.length; i++) { String arg = args[i]; }
4. list循環(huán)
List<String> stringList = new ArrayList<>();
stringList.fori: for (int i = 0; i < stringList.size(); i++) { }
stringList.for: for (String s : stringList) { }
stringList.forr: for (int i = stringList.size() - 1; i >= 0; i--) { }
5. 其他
假設(shè)有這樣的對象
Producer producer = new Producer();
則對象判空:
ifn: if (producer == null) { } inn: if (producer != null) { } // xxx.nnproducer.nn: if (producer != null) { } // xxx.nullproducer.null: if (producer == null) { }
sout:System.out.println();
idea常用模板編程效果:
模板編程: public class TemplateTest { // prsf private static final int a=10; //psf public static final int b=10; //psfi public static final int c=1000; // psfs public static final String d='qqq'; // psvm public static void main(String[] args) { System.out.println('hello'); // soutm System.out.println('TemplateTest.main'); // soutv int n=10; System.out.println('n = ' + n); // xxx.sout int num=100; System.out.println(num); // souf System.out.printf(''); // for循環(huán) //fori for (int i = 0; i <100 ; i++) { // i.sout System.out.println(i); //i.soutv System.out.println('i = ' + i); // i.switch switch (i) { } } // iter for (String arg : args) { } // itar for (int i = 0; i < args.length; i++) { String arg = args[i]; } List<String> stringList = new ArrayList<>(); // stringList.fori for (int i = 0; i < stringList.size(); i++) { } // stringList.for for (String s : stringList) { } // stringList.forr for (int i = stringList.size() - 1; i >= 0; i--) { } Producer producer = new Producer(); // ifn if (producer == null) { } // inn if (producer != null) { } // xxx.nn if (producer != null) { } // xxx.null if (producer == null) { } // inst if (producer instanceof Object) { Object o = (Object) producer; } }}
我們可以通過快捷鍵 ctrl+j 來查看模板編程提示:
更多的idea編程模板可以去Live Templates下面查看
三、模板自定義與修改
我們可以在Live Templates 位置下自改和自定義模板
1. 修改
比如對psfi進行修改
修改前:
psfi: public static final int
修改后:
psfi:public static final int i =
2. 自定義模板
可以通過選擇右邊的+自定義模板,步驟如下:
模板里面的$var$是生成時光標(biāo)停留的位置
點擊define,選擇應(yīng)用范圍(沒有此步驟,模板不生效),這里選擇Java,則勾選Java
自定義效果:
// test public void test(){ }
總結(jié)
到此這篇關(guān)于idea 模板編程的文章就介紹到這了,更多相關(guān)idea 模板編程內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
