On the Java programming ToString () method to rewrite the meaning of _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

Previous article we introduced the significance of java tostring method to rewrite the code examples, then, we simply talk about java programming tostring () method rewritten.

1, toString () is a rewrite, for general objects, it will have this method, in fact, the purpose of this method, the main target is the output of the string out of the way: an analogy, such a People category, inside name, age these two properties,

If you People p = new People ();

p.toString();

To do so, the default output is a memory address.

So you think override the ToString (); This method, according to their own way to output.

The body of such a method toString to: return p.name + p.age;

This time things toString output is the People of the class name and age of.

toString () method is oject, so long as classes are inherited from oject wide to override this method. Application Examples editText.getText (). ToString ()

2, rewriting means, a subclass of a specific implementation of a method modified from the parent class the parent class inheritance, can not modify the name of the method, when System.out.println (object), default toString () method, the the object into a string output, toString () method of recessive inherited from the base class for all classes (Mobile Object class for all classes), if no override certain toString () method, then call toString () obtained ( class name + address name name) this string method name should be changed to another will not go wrong, if you add a rewrite mark, the method name is changed to another can not be called "rewrite" the.

3, when writing Java code generation in the background you want to print the value of a variable to the console, when you print an object when, in fact, call the object's toString () method! This is the class definition phase, properties not initialized, that is, when a print object, the default object .toString print (); to be understood that the output values ​​of all the attributes;

4, the general out.println (Object) and System.out.println (Object), wherein the output is Object.toString () method. Rewrite toString () method, you can output text messages they want

Note: Print an object directly System.out.println (p); in fact, inside the println method will automatically call the p's toString () method.

to sum up

That's all for this article Talking about the significance of the Java programming ToString () method of rewriting, we want to help. Interested friends can see: to use Java AtomicInteger class explain, java tostring method to rewrite the code example, high concurrency solutions Detailed Java systems and so on. What is the problem you can always leave a message, we welcome the discussions and exchange.

The original address is: http: //www.piaodoo.com/thread-13257-1-1.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093720.html