當需要將一個對象輸出到顯示器時,通常要調用他的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