toString,
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
- 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()方法后:
[java] view plaincopy
- 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]]
相关文章
- 暂无相关文章
用户点评