Java Basic Learning Summary--object class

Source: Internet
Author: User

Never give up, everything is possible!!!

Only to find a way to succeed, not to find excuses for failure!

Java Basic Learning Summary--object Class I. Introduction to Object class

the Object class in Java is a special class,Java only supports single inheritance, subclasses can only inherit from a parent class, if the parent class is inherited from another parent class, then he can only have a parent class, the parent class has a parent class, There can only be one,JAVA for organizing this kind of organization is more convenient, it provides a root of the class, the equivalent of all classes are inherited from this class, this class is called Object. So the Object class is the root class of all Java classes and is the ancestor of all Java classes. All classes, no matter who they are, inherit from it.

Ii. Introduction to tostring Method

  

  when a string is connected to another type, the other type is automatically converted to a string type and then concatenated with the string . The underlying data type int,float,double convert to string is simple, according to their numbers converted to become, can reference the type, personp = new Person (); A string plus this p, you don't know how to convert this p to a string, because this p is a reference type.

1PackageCn.galc.test;23PublicClasstesttostring {45PublicStaticvoidMain (string[] args) {6 Dog D =NewDog ();7/**8* If the ToString method is not overridden, the output default string content is "class name + hash code",9* such as: DOG=CN.GALC.TEST.DOG@150BD4D10* Here D is a reference type, when printing, this reference type D automatically calls the ToString () method to convert itself to a string and then to the string "d:=" connected,11* then be printed together. D why the ToString () method can be called automatically, and the dog does not declare the ToString () method.12* This is because the ToString () method is the method inside the object class, and all classes inherit from the object class,13* The dog class is certainly no exception, so the dog inherits the ToString () method inside the object class,14* So the object of the dog class can of course call the ToString () method directly.15* But the dog class is not satisfied with the inherited ToString () method,16* Because using this continuation of the ToString () method to convert a reference object to a string output is a series of confusing hash encodings.17* In order to make the printed information so that normal people can understand, so in the dog class, the inherited ToString () method to rewrite,18* Making it possible to call this ToString () method to convert a reference object into a string when printed out is some normal, readable information.19* When a subclass overrides a method inherited from a parent class, copy the declaration of the method to be overridden from the parent class directly into the subclass,20* This will not be an error when overridden in a subclass.21st*/System.out.println ("dog=" +d);//Print result: Dog=i ' m a cool dog23}24}2526Classdog{27 /**28  * After rewriting the ToString () method inside the object class, 29 30 31 32 */33 public String toString () {34 return "I m a cool Dog" ; }36}       

Any class inherits from the Object class, so you can override the toString () method in any class . the ToString () method is used to automatically invoke the ToString () method when a reference object is connected to a string, or when the reference object is printed directly. This method returns a string representing the normal information of the reference object, and the contents of the string are defined by ourselves, and the default string content is "class name + hash code". So we can change the default string content to the string content of the normal information we want to express by rewriting the toString () method inside the class.

Java Basic Learning Summary--object class

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.