如题

解决方案 »

  1.   

    public class ToStringBuilder
    extends 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.
      

  2.   

    lz参考一下这个:http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ToStringBuilder.html
      

  3.   

    这是apache开源基金会的 commons-lang 项目下的一个类。我正好用过,功能就是在自己定义一个类的toString 方法时,方便的格式化类的属性。public class User {
        private Integer id;
        private String name;
        private String password;   // ...   public String toString() {
            return new ToStringBuilder(this, ToStringStyle.SIMPLE)
                      .append(id)
                      .append(name)
                      .append(password);
       }   public static void main(String[] args) {
              System.out.println(user);
       }
    }如果运行上面的main方法的话,user的属性就可以漂亮的被打印出来了。
      

  4.   

    toString()是object的一个方法,但不怎么有用。许多类在定义时都需要toString()方法。而这需要重写。在用toString()时,可以这样用。比如,Student是一个类,toString()是程序员自己定义的方法。比如s1是一个Student类的对象,你可以这样用:println(s1);下面是我写的一个Complex类。有toString()方法。我也是初学者啊,希望对楼主理解toString有用class MyComplex{
    double real,image;
    public MyComplex(){};
    public MyComplex(double r,double i){
    real=r;
    image=i;
    }
    double getReal(){return real;}
    double getImage(){return image;}
    MyComplex jia(MyComplex a){
    MyComplex temp=new MyComplex();
    temp.image=image+a.image;
    temp.image=image+a.image;
    return temp;
    }
    MyComplex jian(MyComplex a){
    MyComplex temp=new MyComplex();
    temp.image=image-a.image;
    temp.image=image-a.image;
    return temp;
    }
    MyComplex chen(MyComplex a){
    MyComplex temp=new MyComplex();
    temp.image=image*a.image-image*a.image;
    temp.image=real*a.image+image*a.real;
    return temp;
    }
    MyComplex chu(MyComplex a){
    MyComplex temp=new MyComplex();
    temp.image=(image*a.image+image*a.image)/(a.real*a.real+a.image*a.image);
    temp.image=(image*a.real-real*a.image)/(a.real*a.real+a.image*a.image);
    return temp;
    }
    public boolean equals(MyComplex a){
    if(real==a.real&&image==a.image)
    return true;
    else
    return false;
    }
    public String toString(){
    String info=new String();
    if(this.getReal()==0)
    info=String.valueOf(this.getImage())+'i';
    else if(this.getImage()==0)
    info=String.valueOf(this.getReal());
    else if(this.getImage()<0)
    info=String.valueOf(this.getReal())+String.valueOf(this.getImage())+'i';
    else
    info=String.valueOf(this.getReal())+'+'+String.valueOf(this.getImage())+"i";
    return info;
    }
    }class MyComplexTest{
    public static void main(String[] args){
    MyComplex m1=new MyComplex(1,-2);
    MyComplex m2=new MyComplex(1,-2);
    MyComplex m3=new MyComplex(1,-1);
    MyComplex m4=new MyComplex(7.8,-0.8999996);
    MyComplex m5=new MyComplex(-1,16.9);
    if(m1==m2)
    System.out.println("m1==m2=ture");
    else
    System.out.println("m1==m2=false");
    if(m1.equals(m2))
    System.out.println("m1.equals(m2)=ture");
    else
    System.out.println("m1.equals(m2)=false");
    System.out.println(m4);
    System.out.println(m5);
    }
    }
      

  5.   

    首先要弄懂Stringbuilder和String的区别,然后对照着toString方法就好理解了。
    简单的说Stringbuilder可以自由改变字符串,而String不能。。具体效率什么的你可以上网search下;
    其实toString和toStringBuilder方法相似,只是在返回值上有些区别。前者返回类型为String类型,后者为StringBuilder类型,在使用时被重写后可以灵活的改变返回值的长度。
    楼上的是通过重写toString()方法,来实现返回指定的单个字符串。如果变为很多,而且中间还要加一些重复的特殊符号(如Person@14576877[name=pengzhoushuo,age=25,sex=true]中的@,[,],=),那就需要用到toStringBuilder了。当然前者也可以实现,只是麻烦些,而且容易出错,效率低。
    呵呵,希望能对你有帮助!