idea 模板编程知识小结

网友投稿 253 2022-11-27


idea 模板编程知识小结

模板编程是idea的强大功能,也提高了开发人员的编程效率,比如输入main函数:

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编程常用的模板我在上图中标注出来了

二、常用模板

先介绍一下常用的、idea自带的模板

1. static final 变量

prsf: private static final

psf: public static final

psfi: public static final int

psfs: public static final String

2. main函数

psvm/main:

public static void main(String[] args) {

}

3. for循环

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循环

List 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. 其他

假设有这样的对象

Producer producer = new Producer();

则对象判空:

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 StrUTcCVing 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循环

//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 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$是生成时光标停留的位置

点击define,选择应用范围(没有此步骤,模板不生效),这里选择Java,则勾选Java

自定义效果:

// test

public void test(){

}

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:IDEA设置JVM运行参数的方法步骤
下一篇:idea手动刷新git分支的详细教程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~