溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IDEA巧用Postfix Completion讓碼速起飛(小技巧)

發(fā)布時間:2020-09-20 02:30:08 來源:腳本之家 閱讀:235 作者:白菜Java自習(xí)室 欄目:開發(fā)技術(shù)

1. 情景展示

自從做 Java 開發(fā)之后,IDEA 編輯器是不可少的。 在 IDEA 編輯器中,有很多高效的代碼補全功能,尤其是 Postfix Completion 功能,可以讓編寫代碼更加的流暢。

Postfix completion 本質(zhì)上也是代碼補全,它比 Live Templates 在使用上更加流暢一些,我們可以看一下下面的這張圖。

IDEA巧用Postfix Completion讓碼速起飛(小技巧) 

2. 設(shè)置界面

可以通過如下的方法打開 Postfix 的設(shè)置界面,并開啟 Postfix。

IDEA巧用Postfix Completion讓碼速起飛(小技巧) 

3. 常用的 Postfix 模板

3.1. boolean 變量模板

!: Negates boolean expression

//before
public class Foo {
   void m(boolean b) {
     m(b!);
   }
 }
 
//after
public class Foo {
  void m(boolean b) {
    m(!b);
  }
}

if: Checks boolean expression to be 'true'

//before
public class Foo {
  void m(boolean b) {
    b.if
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (b) {

    }
  }
}

else: Checks boolean expression to be 'false'.

//before
public class Foo {
  void m(boolean b) {
    b.else
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (!b) {

    }
  }
}

3.2. array 變量模板

for: Iterates over enumerable collection.

//before
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    values.for
  }
}

//after
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    for (int value : values) {

    }
  }
}

fori: Iterates with index over collection.

//before
public class Foo {
  void m() {
    int foo = 100;
    foo.fori
  }
}

//after
public class Foo {
  void m() {
    int foo = 100;
    for (int i = 0; i < foo; i++) {

    }
  }
}

3.3. 基本類型模板

opt: Creates Optional object.

//before
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 intValue.opt
 doubleValue.opt
 longValue.opt
 objValue.opt
}

//after
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 OptionalInt.of(intValue)
 OptionalDouble.of(doubleValue)
 OptionalLong.of(longValue)
 Optional.ofNullable(objValue)
}

sout: Creates System.out.println call.

//before
public class Foo {
 void m(boolean b) {
  b.sout
 }
}

//after
public class Foo {
 void m(boolean b) {
   System.out.println(b);
 }
}

3.4. Object 模板

nn: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.nn
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

null: Checks expression to be null.

//before
public class Foo {
  void m(Object o) {
    o.null
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

notnull: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.notnull
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

val: Introduces variable for expression.

//before
public class Foo {
  void m(Object o) {
    o instanceof String.var
  }
}

//after
public class Foo {
  void m(Object o) {
    boolean foo = o instanceof String;
  }
}

3.5. 其他模板

new: Inserts new call for the class.

//before
Foo.new

//after
new Foo()

return: Returns value from containing method.

//before
public class Foo {
  String m() {
    "result".return
  }
}
//after
public class Foo {
  String m() {
    return "result";
  }
}

到此這篇關(guān)于IDEA巧用Postfix Completion讓碼速起飛(小技巧)的文章就介紹到這了,更多相關(guān)IDEA Postfix Completion 內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI