Eclipse技巧:1.定制toString()模版
生活随笔
收集整理的這篇文章主要介紹了
Eclipse技巧:1.定制toString()模版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一直對一些問題沒有去深的研究,有人說Intellij好,有人說MyEclipse好,有人說Eclipse好,其實蘿卜青菜各有所愛,只是看大家使用的習慣或者說公司用什么,你不能左右的時候,請去適合周圍的環境,當我們說建立一個類的時候,最后去重寫其toString方法,但是我們是否考慮過生成自己喜歡的風格呢? 下面我就來介紹一下使用Eclipse定制生成toString模版
Eclipse官方文檔
這里我借鑒了 Eclipse官方文檔 文檔說明,稍后再來回顧
Eclipse定制toString模版圖解
-
1.點擊toString方法
-
2.點擊編輯按鈕
-
3.點擊New按鈕設計新的規則
-
4.定制生成Json格式的規則策略
{"className":"{object.getClassName}","{member.name()}":"{member.value}","{otherMembers}"} 復制代碼點擊OK完成,之后需要在toString的頁面設置
-
5.最后一步設置
-
6.生成效果如下
public String toString() {StringBuilder builder = new StringBuilder();builder.append("{\"");if (this.role_id != null) {builder.append("role_id\":\"");builder.append(this.role_id);builder.append("\",\"");}if (this.role_name != null) {builder.append("role_name\":\"");builder.append(this.role_name);builder.append("\",\"");}if (this.role_key != null) {builder.append("role_key\":\"");builder.append(this.role_key);builder.append("\",\"");}if (this.status != null) {builder.append("status\":\"");builder.append(this.status);}builder.append("\"}");return builder.toString();} 復制代碼
模版屬性簡單說明
| ${object.getClassName} | inserts a call to this.getClass.getName() |
| ${object.superToString} | inserts a call to super.toString() |
| ${object.hashCode} | inserts a call to this.hashCode() |
| ${object.identityHashCode} | inserts a call to System.identityHashCode(this) |
| ${member.name} | inserts the first member's name |
| ${member.name()} | inserts the first member's name followed by parenthesis in case of methods |
| ${member.value} | inserts the first member's value |
| ${otherMembers} | inserts the remaining members. For each member, the template fragment between the first and the last {member.*} and {otherMembers} must stand after the last ${member.*} variable). |
總結
以上是生活随笔為你收集整理的Eclipse技巧:1.定制toString()模版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端差异化项目合并打包
- 下一篇: c++ 公有继承的赋值兼容规则