这篇文章主要介绍了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中文网其他相关文章!