toString()工具

public String toString() {
        Field[] fields = this.getClass().getDeclaredFields();
        StringBuffer result = new StringBuffer();
        for (Field field : fields) {
            // System.out.prIntegerln(field.getName());
            String fieldName = field.getName();
            String methodName = "get" + fieldName.substring(0,   1).toUpperCase()
                    + fieldName.substring(1);
            result.append(fieldName + ":");
            Method method = null;
            Object obj = null;
            try {
                method = UserVo.class.getMethod(methodName);
                obj = method.invoke(this, null);
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (obj != null) {
                result.append(obj);
            } else
                result.append("空");
            result.append("\n");
        }
        return result.toString();
    }

 

你可能感兴趣的:(toString)