Example 2:
1 Public classORC2 {3 Public Static classA4 {5 PublicString toString ()6 {7 return"This is A";8 }9 }Ten Public Static voidMain (string[] args) One { AA obj =NewA (); - System.out.println (obj); - } the}//output result!!
Example 2:
1 Public classORC2 {3 Public Static classA4 {5 PublicString getString ()6 {7 return"This is A";8 }9 }Ten Public Static voidMain (string[] args) One { AA obj =NewA (); - System.out.println (obj); - System.out.println (obj.getstring ()); the } -}
See the difference, theadvantage of ToString is that when encountering "println" such as the output method will be automatically called, do not explicitly hit out.
Introduction to a concept
1. Print object and ToString method: The ToString method is that the system will output the object's "self-describing" information to tell the external object that it has state information.
2. The ToString method provided by the object class always returns the class name + @ +hashcode value of the implementation class of the objects.
Two Print object examples
1. Program Example
1 class Person2 {3 PrivateString name;4 PublicPerson (String name)5 {6 This. Name =name;7 }8 }9 Public classPrintObjectTen { One Public Static voidMain (string[] args) A { - //Create a Person object and assign it to the P variable -Person p =NewPerson ("Lin"); the //Print the Person object referenced by P - System.out.println (p); - } -}
2. Operation result
[Email protected]
3. Analysis of results
When you use this method to output a person object, the actual output is the ToString method of the Person object.
Three-Rewrite ToString Method Example
1. Program Example
1 classApple2 {3 PrivateString color;4 Private Doubleweight;5 PublicApple () {}6 //provides a constructor with parameters7 PublicApple (String color,Doubleweight)8 {9 This. color =color;Ten This. Weight =weight; One } A - //color Setter and Getter methods - Public voidsetcolor (String color) the { - This. color =color; - } - PublicString GetColor () + { - return This. Color; + } A at //weight Setter and getter method - Public voidSetweight (Doubleweight) - { - This. Weight =weight; - } - Public Doublegetweight () in { - return This. Weight; to } - //override the ToString method to implement the "self-description" of an Apple object the PublicString toString () * { $ return"An apple, the color is:" +ColorPanax Notoginseng+ ", Weight is:" +weight; - } the + //Public String toString () A // { the //return "apple[color=" + Color + ", weight=" + weight + "]"; + // } - $ } $ Public classtostringtest - { - Public Static voidMain (string[] args) the { -Apple A =NewApple ("Red", 2.38);Wuyi //Print Apple Objects the System.out.println (a); - } Wu}
2. Operation result
An apple, the color is: red, weight is: 2.38
3. Analysis of results
From the running results above, by overriding the Apple class's ToString method, you can have the system print out the "self-describing" information for an Apple object when it is printed.
Crazy Java Handout P167 "6.2 Processing object 6.2.1 Print object and tostring method" slag residue note ctrl+c+v