Home  >  Article  >  Java  >  Analysis on the significance of overriding the ToString() method in Java

Analysis on the significance of overriding the ToString() method in Java

黄舟
黄舟Original
2017-10-12 10:41:061528browse

This article mainly introduces the significance of rewriting the ToString() method in Java programming. It is quite good. I share it with you here for your friends to learn and refer to.

In the previous article, we introduced a code example for rewriting the java tostring method. Next, we will briefly talk about the significance of rewriting the tostring() method in java programming.

1. toString() is rewriting. This method is available for general objects. In fact, the purpose of this method is mainly to output the object as a string: for example, for example A People class has two attributes: name and age.

If you People p = new People();

p.toString();

If you do this, the default output is a memory address.

Then you will think of overriding ToString(); this method outputs in your own way.

For example, change the method body of toString to: return p.name+p.age;

At this time, the output of toString is the name and age of the People class. .

toString() is an object method, so any class that inherits from object can override this method. Application examples editText.getText().toString()

2. Overwriting means that after inheriting from the parent class, the subclass modifies the specific implementation of a method of the parent class, and its method name cannot be modified. , when System.out.println (object), the toString() method is used by default to convert the object into a string output. The toString() method inherits from the implicit base class of all classes (the eldest Object class of all classes), If a certain class does not have an overridden toString() method, then calling toString() will get a string like (class name + address name). Changing it to another method name should not go wrong. Have you added the override? Writing tags and changing them to other method names cannot be called "rewriting".

3. When writing Java code, you want to print the value of a variable to the console in the background generation. When you print an object, you actually call the toString() method of the object. ! This is during the definition stage of the class, and the attributes have not been initialized. That is, when printing an object, the default printing is object.toString(); which can be understood as outputting all attribute values;

4. Generally out .println(Object) and System.out.println(Object), which output the Object.toString() method. Override the toString() method to output the text information you want

Note: To print an object, you can directly System.out.println(p); in fact, p will be automatically called internally in the println method. toString() method.

Summarize

The above is the detailed content of Analysis on the significance of overriding the ToString() method in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn