zoukankan      html  css  js  c++  java
  • Class ToStringBuilder

    ToStringBuilder (Commons Lang 2.4 API)

    org.apache.commons.lang.builder


    Class ToStringBuilder

    java.lang.Object
      extended byorg.apache.commons.lang.builder.ToStringBuilder
    
    Direct Known Subclasses:
    ReflectionToStringBuilder

    public class ToStringBuilder
    extends java.lang.Object

    Assists in implementing Object.toString() methods.

    This class enables a good and consistent toString() to be built for any
    class or object. This class aims to simplify the process by:

    • allowing field names
    • handling all types consistently
    • handling nulls consistently
    • outputting arrays and multi-dimensional arrays
    • enabling the detail level to be controlled for Objects and Collections
    • handling class hierarchies

    To use this class write code as follows:

     public class Person {
       String name;
       int age;
       boolean smoker;
    
       ...
    
       public String toString() {
         return new ToStringBuilder(this).
           append("name", name).
           append("age", age).
           append("smoker", smoker).
           toString();
       }
     }
     

    This will produce a toString of the format:
    Person@7f54[name=Stephen,age=29,smoker=false]

    To add the superclass toString, use appendSuper(java.lang.String).
    To append the toString from an object that is delegated
    to (or any other object), use appendToString(java.lang.String).

    Alternatively, there is a method that uses reflection to determine
    the fields to test. Because these fields are usually private, the method,
    reflectionToString, uses AccessibleObject.setAccessible to
    change the visibility of the fields. This will fail under a security manager,
    unless the appropriate permissions are set up correctly. It is also
    slower than testing explicitly.

    A typical invocation for this method would look like:

     public String toString() {
       return ToStringBuilder.reflectionToString(this);
     }
     

    You can also use the builder to debug 3rd party objects:

     System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
     

    The exact format of the toString is determined by
    the ToStringStyle passed into the constructor.

    Since:
    1.0
    Version:
    $Id: ToStringBuilder.java 492354 2007-01-03 23:48:10Z scolebourne $
    Author:
    Stephen Colebourne, Gary Gregory, Pete Gieser
  • 相关阅读:
    睿象云-智能运维平台
    leetcode-----53. 最大子序和
    leetcode-----50. Pow(x, n)
    leetcode-----49. 字母异位词分组
    leetcode-----48. 旋转图像
    leetcode-----47. 全排列 II
    leetcode-----46. 全排列
    leetcode-----44. 通配符匹配
    SpringMVC @RequestParam和@RequestBody的区别
    SpringMVC 接受页面传递参数
  • 原文地址:https://www.cnblogs.com/lexus/p/2563149.html
Copyright © 2011-2022 走看看