JAVA中重寫toString__JAVA

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.