StringBuilder generates toString

来源:互联网 发布:淘宝美工助理的简介 编辑:程序博客网 时间:2024/05/01 22:57
There is an useful class to help you generate toString.

  1. package com.synnex.jmock.test.usertwo;
  2. import org.apache.commons.lang.builder.ToStringBuilder;
  3. public class PojoTest {
  4.     private String name;
  5.     private int id;
  6.     private int age;
  7.     private String address;
  8.     /**
  9.      * @return the age
  10.      */
  11.     public int getAge() {
  12.         return age;
  13.     }
  14.     /**
  15.      * @param age
  16.      *            the age to set
  17.      */
  18.     public void setAge(int age) {
  19.         this.age = age;
  20.     }
  21.     /**
  22.      * @return the address
  23.      */
  24.     public String getAddress() {
  25.         return address;
  26.     }
  27.     /**
  28.      * @param address
  29.      *            the address to set
  30.      */
  31.     public void setAddress(String address) {
  32.         this.address = address;
  33.     }
  34.     /**
  35.      * @return the name
  36.      */
  37.     public String getName() {
  38.         return name;
  39.     }
  40.     /**
  41.      * @param name
  42.      *            the name to set
  43.      */
  44.     public void setName(String name) {
  45.         this.name = name;
  46.     }
  47.     /**
  48.      * @return the id
  49.      */
  50.     public int getId() {
  51.         return id;
  52.     }
  53.     /**
  54.      * @param id
  55.      *            the id to set
  56.      */
  57.     public void setId(int id) {
  58.         this.id = id;
  59.     }
  60.     /**
  61.      * @param args
  62.      */
  63.     public static void main(String[] args) {
  64.         PojoTest pojo = new PojoTest();
  65.         pojo.setId(1);
  66.         pojo.setName("stefli");
  67.         pojo.setAge(28);
  68.         pojo.setAddress("Tianfu ave. A7 builiding,Tianfu software park");
  69.         System.out.println(pojo.toString());
  70.     }
  71.     public String toString() {
  72.         return ToStringBuilder.reflectionToString(this);
  73.     }
  74. }

原创粉丝点击