IDEA巧用Postfix Completion讓碼速起飛(小技巧)
1. 情景展示
自從做 Java 開發(fā)之后,IDEA 編輯器是不可少的。 在 IDEA 編輯器中,有很多高效的代碼補(bǔ)全功能,尤其是 Postfix Completion 功能,可以讓編寫代碼更加的流暢。
Postfix completion 本質(zhì)上也是代碼補(bǔ)全,它比 Live Templates 在使用上更加流暢一些,我們可以看一下下面的這張圖。
2. 設(shè)置界面
可以通過(guò)如下的方法打開 Postfix 的設(shè)置界面,并開啟 Postfix。
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot整合cxf啟動(dòng)報(bào)錯(cuò),原因是版本問(wèn)題
這篇文章主要介紹了解決springboot整合cxf啟動(dòng)報(bào)錯(cuò),原因是版本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
關(guān)于JDK15的新特性之TextBlocks文本塊的引入和使用
這篇文章主要介紹了關(guān)于JDK15的新特性之文本塊的引入和使用,如果具有一種語(yǔ)言學(xué)機(jī)制,可以比多行文字更直觀地表示字符串,而且可以跨越多行,而且不會(huì)出現(xiàn)轉(zhuǎn)義的視覺(jué)混亂,那么這將提高廣泛Java類程序的可讀性和可寫性,需要的朋友可以參考下2023-07-07
淺談Servlet的Cookie和Session機(jī)制
雖然session機(jī)制在web應(yīng)用程序中被采用已經(jīng)很長(zhǎng)時(shí)間了,但是仍然有很多人不清楚session機(jī)制的本質(zhì),以至不能正確的應(yīng)用這一技術(shù).本文將詳細(xì)討論session以及cookie的工作機(jī)制,需要的朋友可以參考下2021-05-05
詳解IDEA中Debug的使用和進(jìn)制轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了IDEA中Debug的使用和進(jìn)制轉(zhuǎn)換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入
本文主要介紹了MyBatis-Plus 使用枚舉自動(dòng)關(guān)聯(lián)注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
通過(guò)Java計(jì)算文件的MD5值實(shí)現(xiàn)方式
本文將詳細(xì)介紹如何使用Java語(yǔ)言來(lái)計(jì)算文件的MD5值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
分享java打印簡(jiǎn)單圖形的實(shí)現(xiàn)代碼
這篇文章主要分享給大家運(yùn)用java打印簡(jiǎn)單圖形:三角形,菱形,四邊形,需要的朋友可以參考下2015-07-07

