编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

使用idea postfix,快速提高编码效率

wxchong 2024-06-14 13:40:03 开源技术 15 ℃ 0 评论

介绍

idea内置了很多postfix,可以大幅提高编码速度。

首先,注意是否开启了postfix completion。

常用的postfix

  • nn

然后就变成了

if (animal != null) {
            
}
  • animal.null
if (animal == null) {
    
}
  • xx.fori

快速生成升序的for循环。比如100.fori

for (int i = 0; i < 100; i++) {

}
  • xx.forr

快速生成倒序的for循环

for (int i = 100; i > 0; i--) {
    
}
  • xx.field

快速生成字段。比如在有一个Animal类,假定包含一个name字段,需要在构造函数中初始化。

然后就变成了

private static class Animal {

    private final String name;

    public Animal(String name) {
        this.name = name;
    }
}
  • xx.cast

类型转换

int i = 0;
short j = ((short)i);
  • xx.if boolean判断
boolean flag = false;
flag.if==> if (flag) {}
  • xx.inst

类型转换

Animal animal = new Cat("jess", "black");
animal.inst

然后变为:

animal instanceof Cat ? ((Cat)animal) : null;

注意:如果先写赋值操作不行。比如Cat cat = animal.inst,不会生效。

xx.instanceof效果与xx.inst一样。

  • xx.format

String的format操作。

输入:"name: %s ,age: %d".format
输出:String.format("name: %s ,age: %d", "jack", 20)

  • xx.sout

在控制台输出xx。

输入:new Date().sout
输出:System.out.println(new Date());

  • 对数组/集合进行迭代
int[] array = {1, 34, 4, 3, 5};
//array.iter后:
for (int item : array) {
  
}

List<Integer> numbers = new ArrayList<>();
//numbers.iter后:
for (Integer item : numbers) {

}
  • 对象.new

调用对象的构造函数

Cat.new ==> new Cat()

  • xx.opt

用Optional包装

int i=0;
i.opt ==>OptionalInt.of(i);
Cat cat = new Cat("jess", "black");
Optional.of(cat);
  • xx.return

1.return ==> return 1;

  • xx.try

生成try..catch

// 假定有方法m,会抛出异常
private static void m() {
    throw new RuntimeException();
}

// 调用m().try
// 生成下面代码
try {
  m();
} catch (Exception e) {
  e.printStackTrace();
}

结语

idea自带了很多postfix,这里只介绍了我比较常用的一部分。使用好postfix也可以大幅提高编码效率。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表