字符串輸出小技巧

創建一個接收key的類

public class SortKey {

    private String category;
    private Object[] objects;

    public SortKey(String category, Object... objects) {
        this.category = category;
        this.objects = objects;
    }

    @Override
    public String toString() {
        return (new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)).append(this.category).append(this.objects).toString();
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Object[] getObjects() {
        return objects;
    }

    public void setObjects(Object[] objects) {
        this.objects = objects;
    }
}

測試輸出

SortKey sortKey = new SortKey("mama","1","2","3","4","5");
System.out.print(sortKey.toString());

結果:app

SortKey[mama,{1,2,3,4,5}]
相關文章
相關標籤/搜索