最佳的实现toString()方法

public String toString() {
		StringBuffer sb = new StringBuffer();
		Class c = this.getClass();
		Field[] f = c.getDeclaredFields();
		for (int i = 0; i < f.length; i++) {
			f[i].setAccessible(true);
			String fieldName = f[i].getName();
			sb.append(fieldName);
			sb.append("=");
			try {
				sb.append(f[i].get(this));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			sb.append("\n");
		}
		return sb.toString();
	}

你可能感兴趣的:(最佳的实现toString()方法)