Different Java Learning (v) inherit on (1.1) object class ToString ()

Source: Internet
Author: User

After the next equals, we learn the next ToString (),

Java also feels that all objects are not only comparable,

It also enables the object to be printed as a string.

appear



The array was previously shown. The class that this object belongs to is now displayed.

This is followed by the hash value of the object, which means that all objects have a hash value (memory address).

Next. Change Objectdemotostring class

Class Objectdemotostring{public static void Main (string[] args) {Demo d1=new demo (4); System.out.println (Integer.tohexstring (D1.hashcode ()));//hash value converted to 16 binary System.out.println (d1.tostring ());// Print out the D1 object address}}
is an object built to depend on a class file? Yes, not right.

The class file was not encapsulated as an object when it was previously mentioned in memory.

In a class file, polygons have both constructors and general methods, as well as member variables.

If you want to get one of these things, you are not encapsulated as an object.

In this object it is very convenient to define very versatile and not to call.

How to take the constructor is not the most clear object! Yes, not right.


So these objects are all dependent on the class file created,

Class file has been encapsulated object, how is this object represented? See below

Class Objectdemotostring{public static void Main (string[] args) {Demo d1=new demo (4);                Class C=d1.getclass ();//Gets the D1 of the object System.out.println (c); System.out.println (Integer.tohexstring (D1.hashcode ())); System.out.println (D1.tostring ());}}

Results:


These class files have names, constructors,

GetMethods () can get all the definition methods from the. class file

Even defined private methods can be taken,

You do not have to give me the source files are known. This is called anti-compilation.


Class Objectdemo{public static void Main (string[] args) {Demo d1=new demo (4); Class C=d1.getclass (); System.out.println (C.getname () + ' @ ' +integer.tohexstring (D1.hashcode ())); System.out.println (Integer.tohexstring (D1.hashcode ())); System.out.println (D1.tostring ());}}
toString () =getclass (). GetName () + ' @ ' +integer.tohexstring (D1.hashcode ())

Next. Does this address make sense, assuming that you want the corresponding string of the object to be meaningful?


Overwrite in demo ()

Public String toString () {return "demo:" +num;}

Create your own string representation. The default of the parent class does not mean much, it is generally covered

When writing a descriptive narrative, these methods are likely to be overwritten, and then the problem arises.

Assume all overwrite. Why don't you just abstract it and finish it?

After the abstraction is over, object is also abstracted. Well, then, a random definition of a class isn't

Will force you to define the methods that are included, which is not very troublesome.

Besides, have you ever heard of God's abstraction? God does not always live in the hearts of people ~


Different Java Learning (v) inherit on (1.1) object class ToString ()

Related Article

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.