java tostring方法重写代码示例

时间:2022-10-29 14:05:31

当需要将一个对象输出到显示器时,通常要调用他的toString()方法,将对象的内容转换为字符串.java中的所有类默认都有一个toString()方法

默认情况下 System.out.println(对象名)或者System.out.println(对象名.toString())输出的是此对象的类名和此对象对应内存的首地址 如果想自定义输出信息必须重写toString()方法

注意事项

1.必须被声明为public

2.返回类型为String

3.方法的名称必须为toString,且无参数

4.方法体中不要使用输出方法System.out.println()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
public class TreeSetTest {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    SortedSet<Item> parts=new TreeSet<Item>();
    parts.add(new Item("Toaster", 1234));
    parts.add(new Item("Widget", 4562));
    parts.add(new Item("Modem", 9912));
    System.out.println(parts);
    SortedSet<Item> sortByDescription=new TreeSet<Item>(new
        Comparator<Item>()
        {
          public int compare(Item a, Item b)
          {
            String descrA=a.getDescription();
            String descrB=b.getDescription();
            return descrA.compareTo(descrB);
          }
        });
    sortByDescription.addAll(parts);
    System.out.println(sortByDescription);
  }
}
class Item implements Comparable<Item>
{
  public Item(String aDescription, int aPartNumber)
  {
    description=aDescription;
    partNumber=aPartNumber;
  }
  public String getDescription()
  {
    return description;
  }
  public boolean equals(Object otherObject)
  {
    if(this==otherObject)
      return true;
    if(otherObject==null)
    {
      return false;
    }
    if (getClass()!=otherObject.getClass())
    {
      return false;
    }
    Item other=(Item)otherObject;
    return description.equals(other.description)&&
        partNumber==other.partNumber;
  }
  public int hashCode()
  {
    return 13*description.hashCode()+17*partNumber;
  }
  public int compareTo(Item other)
  {
    return partNumber-other.partNumber;
  }
  private String description;
  private int partNumber;
}

输出为:

?
1
2
[Item@8c9e3a56, Item@d780c206, Item@39c021ba]
[Item@39c021ba, Item@8c9e3a56, Item@d780c206]

Item重载toString()方法后:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
public class TreeSetTest {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    SortedSet<Item> parts=new TreeSet<Item>();
    parts.add(new Item("Toaster", 1234));
    parts.add(new Item("Widget", 4562));
    parts.add(new Item("Modem", 9912));
    System.out.println(parts);
    SortedSet<Item> sortByDescription=new TreeSet<Item>(new
        Comparator<Item>()
        {
          public int compare(Item a, Item b)
          {
            String descrA=a.getDescription();
            String descrB=b.getDescription();
            return descrA.compareTo(descrB);
          }
        });
    sortByDescription.addAll(parts);
    System.out.println(sortByDescription);
  }
}
class Item implements Comparable<Item>
{
  public Item(String aDescription, int aPartNumber)
  {
    description=aDescription;
    partNumber=aPartNumber;
  }
  public String getDescription()
  {
    return description;
  }
  public String toString()
  {
    return "[description="+description
        +",partNumber="+partNumber+"]";
  }
  public boolean equals(Object otherObject)
  {
    if(this==otherObject)
      return true;
    if(otherObject==null)
    {
      return false;
    }
    if (getClass()!=otherObject.getClass())
    {
      return false;
    }
    Item other=(Item)otherObject;
    return description.equals(other.description)&&
        partNumber==other.partNumber;
  }
  public int hashCode()
  {
    return 13*description.hashCode()+17*partNumber;
  }
  public int compareTo(Item other)
  {
    return partNumber-other.partNumber;
  }
  private String description;
  private int partNumber;
}

输出为:

?
1
2
[[description=Toaster,partNumber=1234], [description=Widget,partNumber=4562], [description=Modem,partNumber=9912]]
[[description=Modem,partNumber=9912], [description=Toaster,partNumber=1234], [description=Widget,partNumber=4562]]

总结

以上就是本文关于java tostring方法重写代码示例的全部内容,希望对大家有所帮助。有问题您可以留言,欢迎大家交流讨论。

原文链接:http://blog.csdn.net/tzasd89812/article/details/21371661