Java rewrite the meaning of the ToString () method __ "03" Java

Source: Internet
Author: User

1, toString () is to rewrite, for the general object will have this method, in fact, the purpose of this method is to output the object in a string way: for example, a people class, there are name,age these two attributes, if you people p = New people ();
P.tostring ();
In so doing, the default output is a memory address.
Then you will think of rewriting ToString (), and this method, in its own way to output.
For example, the method of ToString modified to: return p.name+p.age;
The thing that ToString outputs at this time is the name and age of the class people.

ToString () is the Oject method, so you can override this method as long as the classes that inherit from Oject are wide. Application examples Edittext.gettext (). ToString ()

2, the rewrite refers to the parent class after inheriting, the subclass of a method of a specific implementation of the modification, can not modify its method name, when the System.out.println (object), the default use of ToString () method, the object to the string output, ToString () Method inherits the implicit base class of all classes (the Cellular object class of all classes), if a class does not override the ToString () method, then calling ToString () will get a string such as the class name + address name, and the other method name should not be wrong, whether you have added an override tag, Changing to a method name cannot be called an "override."

3, in writing Java code, in the background to print the value of variables to the console, when you print an object, is actually called the object's ToString () method. This is in the definition phase of the class, the property has not yet initialized, is the printing object, printing default is the object. toString (); Can be understood as outputting all the attribute values;

4, General out.println (object) and System.out.println (object), where the output is the Object.ToString () method. Rewrite the ToString () method to output the text information you want

Note: Print an object, you can directly System.out.println (p), in fact, the Println method automatically calls the ToString () method.

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.