idea 模板编程 idea 模板编程知识小结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

idea 模板编程 idea 模板编程知识小结

cwfly93   2021-02-03 我要评论
想了解idea 模板编程知识小结的相关内容吗,cwfly93在本文为您仔细讲解idea 模板编程的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:idea,模板编程,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<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. 其他

假设有这样的对象

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 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循环
  //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. 修改

比如对psfi进行修改

修改前:

    psfi: public static final int

修改后:

   psfi:public static final int i = 

2. 自定义模板

可以通过选择右边的+自定义模板,步骤如下:

模板里面的$var$是生成时光标停留的位置

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

自定义效果:

 // test
 public void test(){
  
 }

总结

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们