因项目测试须要,打印出对象的键值对,直接上代码
1 public String toString() { 2 Field[] fields = getClass().getFields(); 3 StringBuffer sb = new StringBuffer(); 4 sb.append(getClass().getName() + ":[/n"); 5 for (Field field : fields) { 6 sb.append(" "); 7 sb.append(field.getName() + " = "); 8 try { 9 sb.append(field.get(this));10 } catch (IllegalArgumentException e) {11 e.PRintStackTrace();12 } catch (IllegalaccessException e) {13 e.printStackTrace();14 }15 sb.append("/n");16 }17 sb.append("]");18 return sb.toString();19 }
效果图:
新闻热点
疑难解答