Eclipse:定製toString()

一直對一些問題沒有去深的研究,有人說Intellij好,有人說MyEclipse好,有人說Eclipse好,其實蘿蔔青菜各有所愛,只是看你們使用的習慣或者說公司用什麼,你不能左右的時候,請去適合周圍的環境,當咱們說創建一個類的時候,最後去重寫其toString方法,可是咱們是否考慮過生成本身喜歡的風格呢? 下面我就來介紹一下使用Eclipse定製生成toString模版java

Eclipse官方文檔

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

定製toString模版圖解

  • 1.點擊toString方法app

  • 2.點擊編輯按鈕eclipse

  • 3.點擊New按鈕設計新的規則jsp

  • 4.定製生成Json格式的規則策略ide

    {"className":"{object.getClassName}","{member.name()}":"{member.value}","{otherMembers}"}  
    複製代碼

    點擊OK完成,以後須要在toString的頁面設置ui

  • 5.最後一步設置this

  • 6.生成效果以下lua

    @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).
相關文章
相關標籤/搜索