一个通用的类的toString()方法

来源:互联网 发布:逆战有没有刷枪软件 编辑:程序博客网 时间:2024/06/09 21:06
public String toString() {
        try {
            Map          map     = BeanUtils.describe(this);
            Iterator     keyIt   = map.keySet().iterator();
            StringBuffer aBuffer = new StringBuffer();

            while (keyIt.hasNext()) {
                String key = (String) keyIt.next();

                if ("class".equals(key)) {
                    continue;
                }

                String value = (String) map.get(key);

                if (aBuffer.length() > 0) {
                    aBuffer.append(", ");
                }

                aBuffer.append(key + " = [" + value + "]");
            }

            return aBuffer.toString();
        } catch (Exception e) {
            return super.toString();
        }
    }

原创粉丝点击