Eclipse使用技巧——自动生成代码

让eclipse的JavaSDK API提示以中文显示

 

刚看见站内一篇文章Eclipse使用技巧 ,于是想把这篇文章说得不的代码模板使用技巧总结下。

 

我要说的java 代码模板是 Windows->Preferences->Java->Editor->Templates 中设置的模板

如果你不在得到什么是eclipse的代码模板,去问google就知道了~~很实用的功能哦!

我搜索到的:eclispe template用法  图文并茂,很适合初学者。

 

 

1.  默认的模板template

templates 面板里已经有许多默认的模板,不熟悉的朋友可以自己研究一下。

比如:输入sysout ,然后 Shift+SPACE 确定出现下面的代码

 System. out .println();

说的不够详细,下次我写过图文的说明~~

 

Template:Alt + /

修改处:Window->Preference->Workbench->Keys->Command->Edit->Content Assist

个人习惯:Shift+SPACE (空白)。

 

 

2.  默认的模板template

templates 面板里已经有许多默认的模板,不熟悉的朋友可以自己研究一下。

比如:输入sysout ,然后 Shift+SPACE 确定出现下面的代码

 

 System. out .println();

说的不够详细,下次我写过图文的说明~~

<!--EndFragment-->

3. 下面都是自定义的模板

name:getlog

 

private   static   final  Log logger =LogFactory.getLog( ${enclosing_type}. class  );

如果你希望把import 也添加进去,比如使用 apachelog 则把模板改为:

 

${: import (org.apache.commons.logging.Log,

        org.apache.commons.logging.LogFactory)}

private   static   final  Log logger = LogFactory.getLog(${enclosing_type}. class );

 

name:debug

 

if  (logger.isDebugEnabled()) {

logger.debug(${Message}, ${exception});

}

 

name:error

 

logger.error(${Message}, ${exception});

name:info

 

logger.info(${Message}, ${exception});

 

name:warn

 

logger.warn(${Message}, ${exception});

 

name:const  常量

 

private static final ${type} ${name} = new ${type} ${cursor};

读文件的模板readfile

 

${: import (java.io.BufferedReader,  

           java.io.FileNotFoundException,  

           java.io.FileReader,  

           java.io.IOException)}  

 BufferedReader in =  null ;  

  try  {  

    in =  new  BufferedReader( new  FileReader(${fileName}));  

    String line;  

     while  ((line = in.readLine()) !=  null ) {  

       ${process}  

    }  

 }  

  catch  (FileNotFoundException e) {  

    logger.error(e) ;  

 }  

  catch  (IOException e) {  

    logger.error(e) ;  

 }  finally  {  

     if (in !=  null ) in.close();  

 }  

 ${cursor}

单例模板singleton 设计模式:

 

/*********************单例************************************/
private static Object syncObj = new Object(); //使用了一个私有静态变量syncObj来保证线程同步
private static ${enclosing_type} instance;
/**
*私有构造方法
*/
private ${enclosing_type}() {
	//TODO 私有构造方法实现:比如初始化
	 ${cursor}
}

 
/**
* Returns this shared instance.
* @returns The shared instance
*/
public static ${enclosing_type} getInstance(){
 if(instance != null){
 return instance;
 }
synchronized (syncObj){ 
if(instance != null) {
return instance; 
}
instance = new ${enclosing_type}();
} 
return instance;
}
/*********************单例************************************/
 

${cursor} :鼠标的位置

也可以根据自己的需要设置各种不同的代码模板,方便快速开发。

还有些模板比如连数据库、其它一些固定模式的代码生成模板就不一一贴出来了~~~

<!--EndFragment-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值