Crazy Java Handout P167 "6.2 Processing object 6.2.1 Print object and tostring method" slag residue note ctrl+c+v

Source: Internet
Author: User
Tags print object

 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

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.