Java Object-oriented (overloaded, overriding common tostring/equals)

Source: Internet
Author: User

Overload (Overload):

* Method name is the same, parameter types are different or the number of parameters is different.

* There is no return value, the return value type and the return value are not the same without affecting the overload.

Example:

 Public classSon { Public voidShowinta)    {System.out.println (a); }     Public voidShow (String s) {System.out.println (s); }     Public voidShow (String s,inta) {System.out.println (a+s); }         Public Static voidMain (string[] args) {son son=NewSon (); Son.show (10); Son.show ("Tim"); Son.show ("AED", 20); }}

Show Results:

Rewrite (overwrite/override):

Redefine the method of the parent class
--in subclasses can be overridden as needed from a method inherited from the parent class

--The overriding method must have the same method name as the overridden method, a parameter list, and a return type

--The overriding method cannot use more restrictive access than the overridden method


Override of ToString Method

System.out.println (s);

System.out.println (s.tostring);

Do not write. ToString is also the ToString output by default

ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:


  

Before overriding ToString

After overriding ToString

Book.java

 Public class Book {    public  String name;      Public String Zuozhe;          Public Book (String name,string zuozhe) {        this. Name = name;          this. Zuozhe = zuozhe;            }      Public String toString () {        return "book [name=" + name + ", zuozhe=" + Zuozhe + "]";    }} 

Main.java

 Public class Main {    publicstaticvoid  main (string[] args) {        =new HashMap ();         new book ("Journey to the Monkey", "Wu Chengen");                          System.out.println (D.tostring ());}        }


Override of the Equals method

The address was compared before the rewrite.

You can define what you compare after you rewrite it.

Book.java

 Public classBook { PublicString name;  PublicString Zuozhe;  PublicBook (String name,string zuozhe) { This. Name =name;  This. Zuozhe =Zuozhe; }     PublicString toString () {return"Book [name=" + name + ", zuozhe=" + Zuozhe + "]"; }     Public Booleanequals (Object aa) {if(AAinstanceofBook ) {Book B=(book) AA; if( This. Name ==b.name && This. Zuozhe = =B.zuozhe) {                return true; }        }        return false; }}

Main.java

 Public class Main {    publicstaticvoid  main (string[] args) {        =new HashMap ();             New Book ("Journey to the Monkey", "Wu Chengen");         New Book ("Journey to the Monkey", "Wu Chengen");                 Boolean bb = b.equals (d);                System.out.println (BB);        }}

Java Object-oriented (overloaded, overriding common tostring/equals)

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.