The toString () method 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 "Im 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 orc = new Orc ("Limburger", 12 );
System. out. println (orc );
Orc. change ("Bob", 19 );
System. out. println (orc );
}
}
Result:
Sample.Orc@11b86e7sample.Orc @ 11b86e7
If the comment is removed, add two toString () methods to obtain
Result:
Orc12: Im a Villain and my name is LimburgerOrc19: Im a Villain and my name is Bob
2. 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 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 #2
3. A general Bean implementing toString ()
When 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 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 testBean = new TestBean ();
TestBean. setId (9 );
System. out. println (testBean. toString ());
}
}
Result
{Id: 9}