欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

toString,

来源: javaer 分享于  点击 31947 次 点评:278

toString,


如果想按照自己需要的 式输出,必须覆盖toString()方法来定义自己的 式。 因为java.lang.Object包中所有的对象都是Object的子类,所以每个对象都有一个toString()。在缺省的情况下,其输出为类名、符号@、对象的hashCode()值getClass().getName() + '@' +Integer.toHexString(hashCode())以下面代码为例:

Public class ToStringWithout {

       int x, y;

       public ToStringWithout(int X, int Y) {

              x = X; y = Y;

       }

       public static void main(String[] args) {

              System.out.println(new ToStringWithout(42, 86));

       }

}

程序将打印一个 式的结果:

ToStringWithout@35ce36

如果想要一个 式化的结果,就需要覆盖toString()方法。这 , 就可以在println()方法、调试器以及其他输出对象的地方,对输出 式进行控制。

public class ToStringWith {

       int x, y;

       public ToStringWith(int X, int Y) {

              x = X; y = Y;

       }

       public String toString() {

              return "ToStringWith[" + x + "," + y + "]";

       }

       public static void main(String[] args) {

              System.out.println(new ToStringWith(42, 86));

       }

}

结果如下:ToStringWith[42,86]

在java中,所有对象都有toString()这个方法,因为它是Object里面已经有了的方法,而所有类都是继承Object,所以“所有对象都有这个方法” 

它通常只是为了方便输出,比如System.out.println(xx),括号里面的“xx”如果不是String类型的话,就自动调用xx的toString()方法 

总而言之,它只是sun公司开发java的时候为了方便所有类的字符串操作而特意加入的一个方法 

例子1: 
public class A{ 
public String toString(){return "this is A";} 

如果某个方法里面有如下句子: 
A obj=new A(); 
System.out.println(obj); 
会得到输出:this is A 

例子2: 
public class A{ 
public String getString(){return "this is A";}//toString改个名字试试看 

A obj=new A(); 
System.out.println(obj); //隐式调用toString()函数
//因为没用自定义toString()函数的返回值,所以会得到输出:xxxx@xxxxxxx的类名加地址形式 ;
System.out.println(obj.getString()); //显式调用getString()函数,会得到输出:this is A 

所以,toString的好处是在碰到“println”之类的输出方法时会自动调用,不用显式打出来

当需要将一个对象输出到显示器时,通常要调用他的toString()方法,将对象的内容转换为字符串.java中的所有类默认都有一个toString()方法

默认情况下 System.out.println(对象名)或者System.out.println(对象名.toString())输出的是此对象的类名和此对象对应内存的首地址 如果想自定义输出信息必须重写toString()方法

注意事项

1.必须被声明为public

2.返回类型为String

3.方法的名称必须为toString,且无参数

4.方法体中不要使用输出方法System.out.println()

[java] view plaincopy

  1. import java.util.*;  
  2. public class TreeSetTest {  
  3.   
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         SortedSet<Item> parts=new TreeSet<Item>();  
  10.         parts.add(new Item("Toaster", 1234));  
  11.         parts.add(new Item("Widget", 4562));  
  12.         parts.add(new Item("Modem", 9912));  
  13.         System.out.println(parts);  
  14.         SortedSet<Item> sortByDescription=new TreeSet<Item>(new   
  15.                 Comparator<Item>()  
  16.                 {  
  17.                     public int compare(Item a, Item b)  
  18.                     {  
  19.                         String descrA=a.getDescription();  
  20.                         String descrB=b.getDescription();  
  21.                         return descrA.compareTo(descrB);  
  22.                     }  
  23.               
  24.                 });  
  25.         sortByDescription.addAll(parts);  
  26.         System.out.println(sortByDescription);  
  27.   
  28.     }  
  29.   
  30. }  
  31. class Item implements Comparable<Item>  
  32. {  
  33.     public Item(String aDescription, int aPartNumber)  
  34.     {  
  35.         description=aDescription;  
  36.         partNumber=aPartNumber;  
  37.     }  
  38.     public String getDescription()  
  39.     {  
  40.         return description;  
  41.     }  
  42.     public boolean equals(Object otherObject)  
  43.     {  
  44.         if(this==otherObject)  
  45.             return true;  
  46.         if(otherObject==null)  
  47.         {  
  48.             return false;  
  49.         }  
  50.         if (getClass()!=otherObject.getClass())  
  51.         {  
  52.             return false;  
  53.         }  
  54.         Item other=(Item)otherObject;  
  55.         return description.equals(other.description)&&  
  56.                 partNumber==other.partNumber;  
  57.     }  
  58.     public int hashCode()  
  59.     {  
  60.         return 13*description.hashCode()+17*partNumber;  
  61.     }  
  62.     public int compareTo(Item other)  
  63.     {  
  64.         return partNumber-other.partNumber;  
  65.     }  
  66.     private String description;  
  67.     private int partNumber;  
  68. }  

输出为:

[Item@8c9e3a56,Item@d780c206, Item@39c021ba]
[Item@39c021ba, Item@8c9e3a56, Item@d780c206]
Item重载toString()方法后:

[java] view plaincopy

  1. import java.util.*;  
  2. public class TreeSetTest {  
  3.   
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         SortedSet<Item> parts=new TreeSet<Item>();  
  10.         parts.add(new Item("Toaster", 1234));  
  11.         parts.add(new Item("Widget", 4562));  
  12.         parts.add(new Item("Modem", 9912));  
  13.         System.out.println(parts);  
  14.         SortedSet<Item> sortByDescription=new TreeSet<Item>(new   
  15.                 Comparator<Item>()  
  16.                 {  
  17.                     public int compare(Item a, Item b)  
  18.                     {  
  19.                         String descrA=a.getDescription();  
  20.                         String descrB=b.getDescription();  
  21.                         return descrA.compareTo(descrB);  
  22.                     }  
  23.               
  24.                 });  
  25.         sortByDescription.addAll(parts);  
  26.         System.out.println(sortByDescription);  
  27.   
  28.     }  
  29.   
  30. }  
  31. class Item implements Comparable<Item>  
  32. {  
  33.     public Item(String aDescription, int aPartNumber)  
  34.     {  
  35.         description=aDescription;  
  36.         partNumber=aPartNumber;  
  37.     }  
  38.     public String getDescription()  
  39.     {  
  40.         return description;  
  41.     }  
  42.     public String toString()  
  43.     {  
  44.         return "[description="+description  
  45.                 +",partNumber="+partNumber+"]";  
  46.     }  
  47.     public boolean equals(Object otherObject)  
  48.     {  
  49.         if(this==otherObject)  
  50.             return true;  
  51.         if(otherObject==null)  
  52.         {  
  53.             return false;  
  54.         }  
  55.         if (getClass()!=otherObject.getClass())  
  56.         {  
  57.             return false;  
  58.         }  
  59.         Item other=(Item)otherObject;  
  60.         return description.equals(other.description)&&  
  61.                 partNumber==other.partNumber;  
  62.     }  
  63.     public int hashCode()  
  64.     {  
  65.         return 13*description.hashCode()+17*partNumber;  
  66.     }  
  67.     public int compareTo(Item other)  
  68.     {  
  69.         return partNumber-other.partNumber;  
  70.     }  
  71.     private String description;  
  72.     private int partNumber;  
  73. }  



输出为:

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

 

 

相关文章

    暂无相关文章

用户点评