Java tostring () Usage

Source: Internet
Author: User
1. Tostring() MethodThe object class has a tostring () method. Each class you create inherits this method. It returns a string representation of the object and is very helpful for debugging. However, the default tostring () method often does not meet the requirements and needs to be overwritten. The tostring () method converts an object to a string. See the following code:
Package sample; Class villain {private string name; protected void set (string nm) {name = Nm;} public villain (string name) {This. name = Name;} Public String tostring () {return "I'm a villain and my name is" + name ;}} public class Orc extends villain {private int orcnumber; public ORC (string name, int orcnumber) {super (name); this. orcnumber = orcnumber;} public void change (string name, int orcnumber) {set (name); this. orcnumber = orcnumber;} Public String tostring () {return "Orc" + orcnumber + ":" + super. tostring ();} public static void main (string [] ARGs) {Orc = new ORC ("Limburger", 12); system. out. println (ORC); Orc. change ("Bob", 19); system. out. println (ORC );}}
Result: If the sample.Orc@11b86e7sample.Orc @ 11b86e7 removes the annotation, that is, after adding two tostring () methods, the result is: orc12: I'm a villain and my name is limburgerorc19: i'm a villain and my name is Bob2. Use tostring () in the container class ()Compile a tool class for outputting iterator on the console.
Import Java. util. iterator; public class printer {static void printall (iterator e) {While (E. hasnext () {system. out. println (E. next ());}}}
Override the tostring () method of the parent class in the hamster class.
Public class hamster {private int hamsternumber; Public hamster (INT hamsternumber) {This. hamsternumber = hamsternumber;} Public String tostring () {return "This Is hamster #" + hamsternumber ;}}
In the hamstermaze class, use the container class to load the hamster Class Object and output the result.
Import Java. util. arraylist; import Java. util. list; public class hamstermaze {@ suppresswarnings ("unchecked") public static void main (string [] ARGs) {list = new arraylist (); For (INT I = 0; I <3; I ++) list. add (new hamster (I); printer. printall (list. iterator ());}}
Result: This is hamster #0 This is hamster #1 This is hamster #23. A tostring () Implementation ()BeanWhen working on a project, we found that many beans need to implement the tostring () method to implement a common bean, and then inherit it through other methods.
Import Java. lang. reflect. field; public class basebean {Public String tostring () {stringbuffer sb = new stringbuffer (); try {class T = This. getclass (); field [] fields = T. getdeclaredfields (); For (INT I = 0; I <fields. length; I ++) {field = Fields [I]; field. setaccessible (true); sb. append ("{"); sb. append (field. getname (); sb. append (":"); If (field. getType () = integer. class) {sb. append (field. getint (this);} else if (field. getType () = long. class) {sb. append (field. getlong (this);} else if (field. getType () = Boolean. class) {sb. append (field. getboolean (this);} else if (field. getType () = char. class) {sb. append (field. getchar (this);} else if (field. getType () = double. class) {sb. append (field. getdouble (this);} else if (field. getType () = float. class) {sb. append (field. getfloat (this);} else sb. append (field. get (this); sb. append ("}") ;}} catch (exception e) {e. printstacktrace ();} return sb. tostring ();}}
Test class
Public class testbean extends basebean {private int ID; Public int GETID () {return ID;} public void setid (int id) {This. id = ID;} public static void main (string [] ARGs) {testbean = new testbean (); testbean. setid (9); system. out. println (testbean. tostring ());}}
Result {ID: 9}
Related Article

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.