11.定制toString方法模版

一直對一些問題沒有去深的研究,有人說Intellij好,有人說MyEclipse好,有人說Eclipse好,其實蘿卜青菜各有所愛,只是看大家使用的習慣或者說公司用什么,你不能左右的時候,請去適合周圍的環(huán)境,當我們說建立一個類的時候,最后去重寫其toString方法,但是我們是否考慮過生成自己喜歡的風格呢? 下面我就來介紹一下使用Eclipse定制生成toString模版

Eclipse官方文檔

這里我借鑒了 https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-dialog-tostring.htm 文檔說明,稍后再來回顧

Eclipse定制toString模版圖解

  • 1.點擊toString方法

    image
  • 2.點擊編輯按鈕

    image
  • 3.點擊New按鈕設(shè)計新的規(guī)則

    image
    image
  • 4.定制生成Json格式的規(guī)則策略

    {"className":"{object.getClassName}","{member.name()}":"{member.value}","{otherMembers}"}  
    
    image

    點擊OK完成,之后需要在toString的頁面設(shè)置

  • 5.最后一步設(shè)置

    image
  • 6.生成效果如下

      @Override
      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.className} inserts the class name as a simple String
${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.*} variable is evaluated and appended to the result. The characters between the last ${member.*} and ${otherMembers} define the separator that is inserted between members (${otherMembers} must stand after the last ${member.*} variable).
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,602評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評論 18 399
  • 先Activity的抽象類 BaseActivity [java]view plaincopy /** *Acti...
    Zaker2Magic閱讀 1,086評論 0 0
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,355評論 0 17
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,017評論 0 11

友情鏈接更多精彩內(nèi)容