overriding Tostring__java in JAVA

Source: Internet
Author: User

When you need to output an object to the display, you typically call his ToString () method to convert the object's contents to a string. All classes in Java have a ToString () method by default

By default, the System.out.println (object name) or System.out.println (object name. ToString ()) Outputs the class name of this object and the first address of this object's corresponding memory. If you want to customize the output information, you must override ToString () Method

Attention matters

1. Must be declared public

2. return type is string

3. The name of the method must be ToString and has no parameters

4. Do not use the output method in the body of the method System.out.println ()

Import java.util.*; public class Treesettest {/** * @param args */public static void main (string[] args) {//TODO auto-generated m
		Ethod 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 Co
						Mpare (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=ad
		Escription;
	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; }
The output is:

[Item@8c9e3a56, item@d780c206, ITEM@39C021BA]
[Item@39c021ba, Item@8c9e3a56, item@d780c206]
After the item overloads the ToString () method:

Import java.util.*; public class Treesettest {/** * @param args */public static void main (string[] args) {//TODO auto-generated m
		Ethod 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 Co
						Mpare (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=ad
		Escription;
	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; }


The output is:

[[description=toaster,partnumber=1234], [description=widget,partnumber=4562], [description=modem,partnumber=9912 ]]
[[description=modem,partnumber=9912], [description=toaster,partnumber=1234], [description=widget,partnumber=4562 ]]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.