Different Java Learning (vi) inheritance under (2.2) object class ToString ()

Source: Internet
Author: User

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

Java also thinks that all objects are not only comparative,

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

appear



Previously shown is an array, which now shows the class to which this object belongs,

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

Next, modify the Objectdemotostring class 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? Right.

Once said in memory, the class file is not encapsulated as an object.

The class file contains both constructors and general methods, as well as member variables.

If you want to get one of those things, you're not going to be wrapped up in an object,

It is very convenient to define many functions within this object.

How to take the constructor is not the most clear object! 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 if you want the string that corresponds to 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 class description, these methods are likely to be overwritten, and then the problem arises.

If you write it all up, simply abstract 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 contained inside, 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 (vi) inheritance under (2.2) object class ToString ()

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.