idea 模板編程知識(shí)小結(jié)
模板編程是idea的強(qiáng)大功能,也提高了開發(fā)人員的編程效率,比如輸入main函數(shù):
public static void main(String[] args){
}
正常情況下我們需要每個(gè)字母挨個(gè)輸入,但是這樣輸入太慢了,有了模板編程,我們只需要輸入psvm或者main,然后回車,就會(huì)輸出
public static void main(String[] args){
}
,是不是大大的提高了編碼速度。這里對(duì)模板編程進(jìn)行簡單的介紹。
一、模板編程簡介
模板編程的位置如下圖: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è)有這樣的對(duì)象
Producer producer = new Producer();
則對(duì)象判空:
ifn:
if (producer == null) {
}
inn:
if (producer != null) {
}
// xxx.nn
producer.nn:
if (producer != null) {
}
// xxx.null
producer.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. 修改
比如對(duì)psfi進(jìn)行修改
修改前:
psfi: public static final int

修改后:
psfi:public static final int i =

2. 自定義模板
可以通過選擇右邊的+自定義模板,步驟如下:


模板里面的$var$是生成時(shí)光標(biāo)停留的位置

點(diǎn)擊define,選擇應(yīng)用范圍(沒有此步驟,模板不生效),這里選擇Java,則勾選Java

自定義效果:
// test
public void test(){
}
總結(jié)
到此這篇關(guān)于idea 模板編程的文章就介紹到這了,更多相關(guān)idea 模板編程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合redis中的JSON序列化文件夾操作小結(jié)
在我們?nèi)粘5捻?xiàng)目開發(fā)中,使用redis作為緩存,來提高系統(tǒng)訪問速度和緩解系統(tǒng)壓力,在使用中遇到幾個(gè)問題,本文給大家詳細(xì)總結(jié)下,對(duì)SpringBoot整合redis?JSON序列化相關(guān)知識(shí)感興趣的朋友一起看看吧2022-02-02
Java中實(shí)現(xiàn)兩個(gè)線程交替運(yùn)行的方法
這篇文章主要介紹了Java中實(shí)現(xiàn)兩個(gè)線程交替運(yùn)行的方法,本文將給大家分享操作流程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
使用Java實(shí)現(xiàn)轉(zhuǎn)換掃描的文檔為可搜索的PDF
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)轉(zhuǎn)換掃描的文檔為可搜索的PDF,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12

