java tostring方法重寫程式碼示例

NO IMAGE

當需要將一個物件輸出到顯示器時,通常要呼叫他的toString()方法,將物件的內容轉換為字串.java中的所有類預設都有一個toString()方法

預設情況下 System.out.println(物件名)或者System.out.println(物件名.toString())輸出的是此物件的類名和此物件對應記憶體的首地址 如果想自定義輸出資訊必須重寫toString()方法

注意事項

1.必須被宣告為public

2.返回型別為String

3.方法的名稱必須為toString,且無引數

4.方法體中不要使用輸出方法System.out.println()


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; 
} 

輸出為:


[[email protected], [email protected], [email protected]]
[[email protected], [email protected], [email protected]]

Item過載toString()方法後:


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; 
} 

輸出為:


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

總結