這篇文章主要介紹了java tostring方法重寫程式碼範例,具有一定參考價值,需要的朋友可以了解下。
當需要將一個物件輸出到顯示器時,通常要呼叫他的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; }
輸出為:
[Item@8c9e3a56, Item@d780c206, Item@39c021ba] [Item@39c021ba, Item@8c9e3a56, Item@d780c206]
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]]
總結
以上是Java中tostring方法重寫的程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!